drbd_proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. drbd_proc.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/fs.h>
  22. #include <linux/file.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/drbd.h>
  26. #include "drbd_int.h"
  27. static int drbd_proc_open(struct inode *inode, struct file *file);
  28. static int drbd_proc_release(struct inode *inode, struct file *file);
  29. struct proc_dir_entry *drbd_proc;
  30. const struct file_operations drbd_proc_fops = {
  31. .owner = THIS_MODULE,
  32. .open = drbd_proc_open,
  33. .read = seq_read,
  34. .llseek = seq_lseek,
  35. .release = drbd_proc_release,
  36. };
  37. static void seq_printf_with_thousands_grouping(struct seq_file *seq, long v)
  38. {
  39. /* v is in kB/sec. We don't expect TiByte/sec yet. */
  40. if (unlikely(v >= 1000000)) {
  41. /* cool: > GiByte/s */
  42. seq_printf(seq, "%ld,", v / 1000000);
  43. v %= 1000000;
  44. seq_printf(seq, "%03ld,%03ld", v/1000, v % 1000);
  45. } else if (likely(v >= 1000))
  46. seq_printf(seq, "%ld,%03ld", v/1000, v % 1000);
  47. else
  48. seq_printf(seq, "%ld", v);
  49. }
  50. static void drbd_get_syncer_progress(struct drbd_device *device,
  51. union drbd_dev_state state, unsigned long *rs_total,
  52. unsigned long *bits_left, unsigned int *per_mil_done)
  53. {
  54. /* this is to break it at compile time when we change that, in case we
  55. * want to support more than (1<<32) bits on a 32bit arch. */
  56. typecheck(unsigned long, device->rs_total);
  57. *rs_total = device->rs_total;
  58. /* note: both rs_total and rs_left are in bits, i.e. in
  59. * units of BM_BLOCK_SIZE.
  60. * for the percentage, we don't care. */
  61. if (state.conn == C_VERIFY_S || state.conn == C_VERIFY_T)
  62. *bits_left = device->ov_left;
  63. else
  64. *bits_left = drbd_bm_total_weight(device) - device->rs_failed;
  65. /* >> 10 to prevent overflow,
  66. * +1 to prevent division by zero */
  67. if (*bits_left > *rs_total) {
  68. /* D'oh. Maybe a logic bug somewhere. More likely just a race
  69. * between state change and reset of rs_total.
  70. */
  71. *bits_left = *rs_total;
  72. *per_mil_done = *rs_total ? 0 : 1000;
  73. } else {
  74. /* Make sure the division happens in long context.
  75. * We allow up to one petabyte storage right now,
  76. * at a granularity of 4k per bit that is 2**38 bits.
  77. * After shift right and multiplication by 1000,
  78. * this should still fit easily into a 32bit long,
  79. * so we don't need a 64bit division on 32bit arch.
  80. * Note: currently we don't support such large bitmaps on 32bit
  81. * arch anyways, but no harm done to be prepared for it here.
  82. */
  83. unsigned int shift = *rs_total > UINT_MAX ? 16 : 10;
  84. unsigned long left = *bits_left >> shift;
  85. unsigned long total = 1UL + (*rs_total >> shift);
  86. unsigned long tmp = 1000UL - left * 1000UL/total;
  87. *per_mil_done = tmp;
  88. }
  89. }
  90. /*lge
  91. * progress bars shamelessly adapted from driver/md/md.c
  92. * output looks like
  93. * [=====>..............] 33.5% (23456/123456)
  94. * finish: 2:20:20 speed: 6,345 (6,456) K/sec
  95. */
  96. static void drbd_syncer_progress(struct drbd_device *device, struct seq_file *seq,
  97. union drbd_dev_state state)
  98. {
  99. unsigned long db, dt, dbdt, rt, rs_total, rs_left;
  100. unsigned int res;
  101. int i, x, y;
  102. int stalled = 0;
  103. drbd_get_syncer_progress(device, state, &rs_total, &rs_left, &res);
  104. x = res/50;
  105. y = 20-x;
  106. seq_printf(seq, "\t[");
  107. for (i = 1; i < x; i++)
  108. seq_printf(seq, "=");
  109. seq_printf(seq, ">");
  110. for (i = 0; i < y; i++)
  111. seq_printf(seq, ".");
  112. seq_printf(seq, "] ");
  113. if (state.conn == C_VERIFY_S || state.conn == C_VERIFY_T)
  114. seq_printf(seq, "verified:");
  115. else
  116. seq_printf(seq, "sync'ed:");
  117. seq_printf(seq, "%3u.%u%% ", res / 10, res % 10);
  118. /* if more than a few GB, display in MB */
  119. if (rs_total > (4UL << (30 - BM_BLOCK_SHIFT)))
  120. seq_printf(seq, "(%lu/%lu)M",
  121. (unsigned long) Bit2KB(rs_left >> 10),
  122. (unsigned long) Bit2KB(rs_total >> 10));
  123. else
  124. seq_printf(seq, "(%lu/%lu)K",
  125. (unsigned long) Bit2KB(rs_left),
  126. (unsigned long) Bit2KB(rs_total));
  127. seq_printf(seq, "\n\t");
  128. /* see drivers/md/md.c
  129. * We do not want to overflow, so the order of operands and
  130. * the * 100 / 100 trick are important. We do a +1 to be
  131. * safe against division by zero. We only estimate anyway.
  132. *
  133. * dt: time from mark until now
  134. * db: blocks written from mark until now
  135. * rt: remaining time
  136. */
  137. /* Rolling marks. last_mark+1 may just now be modified. last_mark+2 is
  138. * at least (DRBD_SYNC_MARKS-2)*DRBD_SYNC_MARK_STEP old, and has at
  139. * least DRBD_SYNC_MARK_STEP time before it will be modified. */
  140. /* ------------------------ ~18s average ------------------------ */
  141. i = (device->rs_last_mark + 2) % DRBD_SYNC_MARKS;
  142. dt = (jiffies - device->rs_mark_time[i]) / HZ;
  143. if (dt > 180)
  144. stalled = 1;
  145. if (!dt)
  146. dt++;
  147. db = device->rs_mark_left[i] - rs_left;
  148. rt = (dt * (rs_left / (db/100+1)))/100; /* seconds */
  149. seq_printf(seq, "finish: %lu:%02lu:%02lu",
  150. rt / 3600, (rt % 3600) / 60, rt % 60);
  151. dbdt = Bit2KB(db/dt);
  152. seq_printf(seq, " speed: ");
  153. seq_printf_with_thousands_grouping(seq, dbdt);
  154. seq_printf(seq, " (");
  155. /* ------------------------- ~3s average ------------------------ */
  156. if (proc_details >= 1) {
  157. /* this is what drbd_rs_should_slow_down() uses */
  158. i = (device->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
  159. dt = (jiffies - device->rs_mark_time[i]) / HZ;
  160. if (!dt)
  161. dt++;
  162. db = device->rs_mark_left[i] - rs_left;
  163. dbdt = Bit2KB(db/dt);
  164. seq_printf_with_thousands_grouping(seq, dbdt);
  165. seq_printf(seq, " -- ");
  166. }
  167. /* --------------------- long term average ---------------------- */
  168. /* mean speed since syncer started
  169. * we do account for PausedSync periods */
  170. dt = (jiffies - device->rs_start - device->rs_paused) / HZ;
  171. if (dt == 0)
  172. dt = 1;
  173. db = rs_total - rs_left;
  174. dbdt = Bit2KB(db/dt);
  175. seq_printf_with_thousands_grouping(seq, dbdt);
  176. seq_printf(seq, ")");
  177. if (state.conn == C_SYNC_TARGET ||
  178. state.conn == C_VERIFY_S) {
  179. seq_printf(seq, " want: ");
  180. seq_printf_with_thousands_grouping(seq, device->c_sync_rate);
  181. }
  182. seq_printf(seq, " K/sec%s\n", stalled ? " (stalled)" : "");
  183. if (proc_details >= 1) {
  184. /* 64 bit:
  185. * we convert to sectors in the display below. */
  186. unsigned long bm_bits = drbd_bm_bits(device);
  187. unsigned long bit_pos;
  188. unsigned long long stop_sector = 0;
  189. if (state.conn == C_VERIFY_S ||
  190. state.conn == C_VERIFY_T) {
  191. bit_pos = bm_bits - device->ov_left;
  192. if (verify_can_do_stop_sector(device))
  193. stop_sector = device->ov_stop_sector;
  194. } else
  195. bit_pos = device->bm_resync_fo;
  196. /* Total sectors may be slightly off for oddly
  197. * sized devices. So what. */
  198. seq_printf(seq,
  199. "\t%3d%% sector pos: %llu/%llu",
  200. (int)(bit_pos / (bm_bits/100+1)),
  201. (unsigned long long)bit_pos * BM_SECT_PER_BIT,
  202. (unsigned long long)bm_bits * BM_SECT_PER_BIT);
  203. if (stop_sector != 0 && stop_sector != ULLONG_MAX)
  204. seq_printf(seq, " stop sector: %llu", stop_sector);
  205. seq_printf(seq, "\n");
  206. }
  207. }
  208. static int drbd_seq_show(struct seq_file *seq, void *v)
  209. {
  210. int i, prev_i = -1;
  211. const char *sn;
  212. struct drbd_device *device;
  213. struct net_conf *nc;
  214. union drbd_dev_state state;
  215. char wp;
  216. static char write_ordering_chars[] = {
  217. [WO_none] = 'n',
  218. [WO_drain_io] = 'd',
  219. [WO_bdev_flush] = 'f',
  220. };
  221. seq_printf(seq, "version: " REL_VERSION " (api:%d/proto:%d-%d)\n%s\n",
  222. API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX, drbd_buildtag());
  223. /*
  224. cs .. connection state
  225. ro .. node role (local/remote)
  226. ds .. disk state (local/remote)
  227. protocol
  228. various flags
  229. ns .. network send
  230. nr .. network receive
  231. dw .. disk write
  232. dr .. disk read
  233. al .. activity log write count
  234. bm .. bitmap update write count
  235. pe .. pending (waiting for ack or data reply)
  236. ua .. unack'd (still need to send ack or data reply)
  237. ap .. application requests accepted, but not yet completed
  238. ep .. number of epochs currently "on the fly", P_BARRIER_ACK pending
  239. wo .. write ordering mode currently in use
  240. oos .. known out-of-sync kB
  241. */
  242. rcu_read_lock();
  243. idr_for_each_entry(&drbd_devices, device, i) {
  244. if (prev_i != i - 1)
  245. seq_printf(seq, "\n");
  246. prev_i = i;
  247. state = device->state;
  248. sn = drbd_conn_str(state.conn);
  249. if (state.conn == C_STANDALONE &&
  250. state.disk == D_DISKLESS &&
  251. state.role == R_SECONDARY) {
  252. seq_printf(seq, "%2d: cs:Unconfigured\n", i);
  253. } else {
  254. /* reset device->congestion_reason */
  255. bdi_rw_congested(&device->rq_queue->backing_dev_info);
  256. nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
  257. wp = nc ? nc->wire_protocol - DRBD_PROT_A + 'A' : ' ';
  258. seq_printf(seq,
  259. "%2d: cs:%s ro:%s/%s ds:%s/%s %c %c%c%c%c%c%c\n"
  260. " ns:%u nr:%u dw:%u dr:%u al:%u bm:%u "
  261. "lo:%d pe:%d ua:%d ap:%d ep:%d wo:%c",
  262. i, sn,
  263. drbd_role_str(state.role),
  264. drbd_role_str(state.peer),
  265. drbd_disk_str(state.disk),
  266. drbd_disk_str(state.pdsk),
  267. wp,
  268. drbd_suspended(device) ? 's' : 'r',
  269. state.aftr_isp ? 'a' : '-',
  270. state.peer_isp ? 'p' : '-',
  271. state.user_isp ? 'u' : '-',
  272. device->congestion_reason ?: '-',
  273. test_bit(AL_SUSPENDED, &device->flags) ? 's' : '-',
  274. device->send_cnt/2,
  275. device->recv_cnt/2,
  276. device->writ_cnt/2,
  277. device->read_cnt/2,
  278. device->al_writ_cnt,
  279. device->bm_writ_cnt,
  280. atomic_read(&device->local_cnt),
  281. atomic_read(&device->ap_pending_cnt) +
  282. atomic_read(&device->rs_pending_cnt),
  283. atomic_read(&device->unacked_cnt),
  284. atomic_read(&device->ap_bio_cnt),
  285. first_peer_device(device)->connection->epochs,
  286. write_ordering_chars[device->resource->write_ordering]
  287. );
  288. seq_printf(seq, " oos:%llu\n",
  289. Bit2KB((unsigned long long)
  290. drbd_bm_total_weight(device)));
  291. }
  292. if (state.conn == C_SYNC_SOURCE ||
  293. state.conn == C_SYNC_TARGET ||
  294. state.conn == C_VERIFY_S ||
  295. state.conn == C_VERIFY_T)
  296. drbd_syncer_progress(device, seq, state);
  297. if (proc_details >= 1 && get_ldev_if_state(device, D_FAILED)) {
  298. lc_seq_printf_stats(seq, device->resync);
  299. lc_seq_printf_stats(seq, device->act_log);
  300. put_ldev(device);
  301. }
  302. if (proc_details >= 2)
  303. seq_printf(seq, "\tblocked on activity log: %d\n", atomic_read(&device->ap_actlog_cnt));
  304. }
  305. rcu_read_unlock();
  306. return 0;
  307. }
  308. static int drbd_proc_open(struct inode *inode, struct file *file)
  309. {
  310. int err;
  311. if (try_module_get(THIS_MODULE)) {
  312. err = single_open(file, drbd_seq_show, NULL);
  313. if (err)
  314. module_put(THIS_MODULE);
  315. return err;
  316. }
  317. return -ENODEV;
  318. }
  319. static int drbd_proc_release(struct inode *inode, struct file *file)
  320. {
  321. module_put(THIS_MODULE);
  322. return single_release(inode, file);
  323. }
  324. /* PROC FS stuff end */