drbd_proc.c 9.8 KB

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