debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/device.h>
  4. #include <linux/slab.h>
  5. #include <linux/module.h>
  6. #include <linux/ctype.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/math64.h>
  10. #include <linux/ktime.h>
  11. #include <linux/ceph/libceph.h>
  12. #include <linux/ceph/mon_client.h>
  13. #include <linux/ceph/auth.h>
  14. #include <linux/ceph/debugfs.h>
  15. #include "super.h"
  16. #ifdef CONFIG_DEBUG_FS
  17. #include "mds_client.h"
  18. #include "metric.h"
  19. static int mdsmap_show(struct seq_file *s, void *p)
  20. {
  21. int i;
  22. struct ceph_fs_client *fsc = s->private;
  23. struct ceph_mdsmap *mdsmap;
  24. if (!fsc->mdsc || !fsc->mdsc->mdsmap)
  25. return 0;
  26. mdsmap = fsc->mdsc->mdsmap;
  27. seq_printf(s, "epoch %d\n", mdsmap->m_epoch);
  28. seq_printf(s, "root %d\n", mdsmap->m_root);
  29. seq_printf(s, "max_mds %d\n", mdsmap->m_max_mds);
  30. seq_printf(s, "session_timeout %d\n", mdsmap->m_session_timeout);
  31. seq_printf(s, "session_autoclose %d\n", mdsmap->m_session_autoclose);
  32. for (i = 0; i < mdsmap->possible_max_rank; i++) {
  33. struct ceph_entity_addr *addr = &mdsmap->m_info[i].addr;
  34. int state = mdsmap->m_info[i].state;
  35. seq_printf(s, "\tmds%d\t%s\t(%s)\n", i,
  36. ceph_pr_addr(addr),
  37. ceph_mds_state_name(state));
  38. }
  39. return 0;
  40. }
  41. /*
  42. * mdsc debugfs
  43. */
  44. static int mdsc_show(struct seq_file *s, void *p)
  45. {
  46. struct ceph_fs_client *fsc = s->private;
  47. struct ceph_mds_client *mdsc = fsc->mdsc;
  48. struct ceph_mds_request *req;
  49. struct rb_node *rp;
  50. int pathlen = 0;
  51. u64 pathbase;
  52. char *path;
  53. mutex_lock(&mdsc->mutex);
  54. for (rp = rb_first(&mdsc->request_tree); rp; rp = rb_next(rp)) {
  55. req = rb_entry(rp, struct ceph_mds_request, r_node);
  56. if (req->r_request && req->r_session)
  57. seq_printf(s, "%lld\tmds%d\t", req->r_tid,
  58. req->r_session->s_mds);
  59. else if (!req->r_request)
  60. seq_printf(s, "%lld\t(no request)\t", req->r_tid);
  61. else
  62. seq_printf(s, "%lld\t(no session)\t", req->r_tid);
  63. seq_printf(s, "%s", ceph_mds_op_name(req->r_op));
  64. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  65. seq_puts(s, "\t(unsafe)");
  66. else
  67. seq_puts(s, "\t");
  68. if (req->r_inode) {
  69. seq_printf(s, " #%llx", ceph_ino(req->r_inode));
  70. } else if (req->r_dentry) {
  71. path = ceph_mdsc_build_path(req->r_dentry, &pathlen,
  72. &pathbase, 0);
  73. if (IS_ERR(path))
  74. path = NULL;
  75. spin_lock(&req->r_dentry->d_lock);
  76. seq_printf(s, " #%llx/%pd (%s)",
  77. ceph_ino(d_inode(req->r_dentry->d_parent)),
  78. req->r_dentry,
  79. path ? path : "");
  80. spin_unlock(&req->r_dentry->d_lock);
  81. ceph_mdsc_free_path(path, pathlen);
  82. } else if (req->r_path1) {
  83. seq_printf(s, " #%llx/%s", req->r_ino1.ino,
  84. req->r_path1);
  85. } else {
  86. seq_printf(s, " #%llx", req->r_ino1.ino);
  87. }
  88. if (req->r_old_dentry) {
  89. path = ceph_mdsc_build_path(req->r_old_dentry, &pathlen,
  90. &pathbase, 0);
  91. if (IS_ERR(path))
  92. path = NULL;
  93. spin_lock(&req->r_old_dentry->d_lock);
  94. seq_printf(s, " #%llx/%pd (%s)",
  95. req->r_old_dentry_dir ?
  96. ceph_ino(req->r_old_dentry_dir) : 0,
  97. req->r_old_dentry,
  98. path ? path : "");
  99. spin_unlock(&req->r_old_dentry->d_lock);
  100. ceph_mdsc_free_path(path, pathlen);
  101. } else if (req->r_path2 && req->r_op != CEPH_MDS_OP_SYMLINK) {
  102. if (req->r_ino2.ino)
  103. seq_printf(s, " #%llx/%s", req->r_ino2.ino,
  104. req->r_path2);
  105. else
  106. seq_printf(s, " %s", req->r_path2);
  107. }
  108. seq_puts(s, "\n");
  109. }
  110. mutex_unlock(&mdsc->mutex);
  111. return 0;
  112. }
  113. #define CEPH_LAT_METRIC_SHOW(name, total, avg, min, max, sq) { \
  114. s64 _total, _avg, _min, _max, _sq, _st; \
  115. _avg = ktime_to_us(avg); \
  116. _min = ktime_to_us(min == KTIME_MAX ? 0 : min); \
  117. _max = ktime_to_us(max); \
  118. _total = total - 1; \
  119. _sq = _total > 0 ? DIV64_U64_ROUND_CLOSEST(sq, _total) : 0; \
  120. _st = int_sqrt64(_sq); \
  121. _st = ktime_to_us(_st); \
  122. seq_printf(s, "%-14s%-12lld%-16lld%-16lld%-16lld%lld\n", \
  123. name, total, _avg, _min, _max, _st); \
  124. }
  125. #define CEPH_SZ_METRIC_SHOW(name, total, avg, min, max, sum) { \
  126. u64 _min = min == U64_MAX ? 0 : min; \
  127. seq_printf(s, "%-14s%-12lld%-16llu%-16llu%-16llu%llu\n", \
  128. name, total, avg, _min, max, sum); \
  129. }
  130. static int metrics_file_show(struct seq_file *s, void *p)
  131. {
  132. struct ceph_fs_client *fsc = s->private;
  133. struct ceph_client_metric *m = &fsc->mdsc->metric;
  134. seq_printf(s, "item total\n");
  135. seq_printf(s, "------------------------------------------\n");
  136. seq_printf(s, "%-35s%lld\n", "total inodes",
  137. percpu_counter_sum(&m->total_inodes));
  138. seq_printf(s, "%-35s%lld\n", "opened files",
  139. atomic64_read(&m->opened_files));
  140. seq_printf(s, "%-35s%lld\n", "pinned i_caps",
  141. atomic64_read(&m->total_caps));
  142. seq_printf(s, "%-35s%lld\n", "opened inodes",
  143. percpu_counter_sum(&m->opened_inodes));
  144. return 0;
  145. }
  146. static const char * const metric_str[] = {
  147. "read",
  148. "write",
  149. "metadata",
  150. "copyfrom"
  151. };
  152. static int metrics_latency_show(struct seq_file *s, void *p)
  153. {
  154. struct ceph_fs_client *fsc = s->private;
  155. struct ceph_client_metric *cm = &fsc->mdsc->metric;
  156. struct ceph_metric *m;
  157. s64 total, avg, min, max, sq;
  158. int i;
  159. seq_printf(s, "item total avg_lat(us) min_lat(us) max_lat(us) stdev(us)\n");
  160. seq_printf(s, "-----------------------------------------------------------------------------------\n");
  161. for (i = 0; i < METRIC_MAX; i++) {
  162. m = &cm->metric[i];
  163. spin_lock(&m->lock);
  164. total = m->total;
  165. avg = m->latency_avg;
  166. min = m->latency_min;
  167. max = m->latency_max;
  168. sq = m->latency_sq_sum;
  169. spin_unlock(&m->lock);
  170. CEPH_LAT_METRIC_SHOW(metric_str[i], total, avg, min, max, sq);
  171. }
  172. return 0;
  173. }
  174. static int metrics_size_show(struct seq_file *s, void *p)
  175. {
  176. struct ceph_fs_client *fsc = s->private;
  177. struct ceph_client_metric *cm = &fsc->mdsc->metric;
  178. struct ceph_metric *m;
  179. s64 total;
  180. u64 sum, avg, min, max;
  181. int i;
  182. seq_printf(s, "item total avg_sz(bytes) min_sz(bytes) max_sz(bytes) total_sz(bytes)\n");
  183. seq_printf(s, "----------------------------------------------------------------------------------------\n");
  184. for (i = 0; i < METRIC_MAX; i++) {
  185. /* skip 'metadata' as it doesn't use the size metric */
  186. if (i == METRIC_METADATA)
  187. continue;
  188. m = &cm->metric[i];
  189. spin_lock(&m->lock);
  190. total = m->total;
  191. sum = m->size_sum;
  192. avg = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum, total) : 0;
  193. min = m->size_min;
  194. max = m->size_max;
  195. spin_unlock(&m->lock);
  196. CEPH_SZ_METRIC_SHOW(metric_str[i], total, avg, min, max, sum);
  197. }
  198. return 0;
  199. }
  200. static int metrics_caps_show(struct seq_file *s, void *p)
  201. {
  202. struct ceph_fs_client *fsc = s->private;
  203. struct ceph_client_metric *m = &fsc->mdsc->metric;
  204. int nr_caps = 0;
  205. seq_printf(s, "item total miss hit\n");
  206. seq_printf(s, "-------------------------------------------------\n");
  207. seq_printf(s, "%-14s%-16lld%-16lld%lld\n", "d_lease",
  208. atomic64_read(&m->total_dentries),
  209. percpu_counter_sum(&m->d_lease_mis),
  210. percpu_counter_sum(&m->d_lease_hit));
  211. nr_caps = atomic64_read(&m->total_caps);
  212. seq_printf(s, "%-14s%-16d%-16lld%lld\n", "caps", nr_caps,
  213. percpu_counter_sum(&m->i_caps_mis),
  214. percpu_counter_sum(&m->i_caps_hit));
  215. return 0;
  216. }
  217. static int caps_show_cb(struct inode *inode, int mds, void *p)
  218. {
  219. struct ceph_inode_info *ci = ceph_inode(inode);
  220. struct seq_file *s = p;
  221. struct ceph_cap *cap;
  222. spin_lock(&ci->i_ceph_lock);
  223. cap = __get_cap_for_mds(ci, mds);
  224. if (cap)
  225. seq_printf(s, "0x%-17llx%-3d%-17s%-17s\n", ceph_ino(inode),
  226. cap->session->s_mds,
  227. ceph_cap_string(cap->issued),
  228. ceph_cap_string(cap->implemented));
  229. spin_unlock(&ci->i_ceph_lock);
  230. return 0;
  231. }
  232. static int caps_show(struct seq_file *s, void *p)
  233. {
  234. struct ceph_fs_client *fsc = s->private;
  235. struct ceph_mds_client *mdsc = fsc->mdsc;
  236. int total, avail, used, reserved, min, i;
  237. struct cap_wait *cw;
  238. ceph_reservation_status(fsc, &total, &avail, &used, &reserved, &min);
  239. seq_printf(s, "total\t\t%d\n"
  240. "avail\t\t%d\n"
  241. "used\t\t%d\n"
  242. "reserved\t%d\n"
  243. "min\t\t%d\n\n",
  244. total, avail, used, reserved, min);
  245. seq_printf(s, "ino mds issued implemented\n");
  246. seq_printf(s, "--------------------------------------------------\n");
  247. mutex_lock(&mdsc->mutex);
  248. for (i = 0; i < mdsc->max_sessions; i++) {
  249. struct ceph_mds_session *session;
  250. session = __ceph_lookup_mds_session(mdsc, i);
  251. if (!session)
  252. continue;
  253. mutex_unlock(&mdsc->mutex);
  254. mutex_lock(&session->s_mutex);
  255. ceph_iterate_session_caps(session, caps_show_cb, s);
  256. mutex_unlock(&session->s_mutex);
  257. ceph_put_mds_session(session);
  258. mutex_lock(&mdsc->mutex);
  259. }
  260. mutex_unlock(&mdsc->mutex);
  261. seq_printf(s, "\n\nWaiters:\n--------\n");
  262. seq_printf(s, "tgid ino need want\n");
  263. seq_printf(s, "-----------------------------------------------------\n");
  264. spin_lock(&mdsc->caps_list_lock);
  265. list_for_each_entry(cw, &mdsc->cap_wait_list, list) {
  266. seq_printf(s, "%-13d0x%-17llx%-17s%-17s\n", cw->tgid, cw->ino,
  267. ceph_cap_string(cw->need),
  268. ceph_cap_string(cw->want));
  269. }
  270. spin_unlock(&mdsc->caps_list_lock);
  271. return 0;
  272. }
  273. static int mds_sessions_show(struct seq_file *s, void *ptr)
  274. {
  275. struct ceph_fs_client *fsc = s->private;
  276. struct ceph_mds_client *mdsc = fsc->mdsc;
  277. struct ceph_auth_client *ac = fsc->client->monc.auth;
  278. struct ceph_options *opt = fsc->client->options;
  279. int mds;
  280. mutex_lock(&mdsc->mutex);
  281. /* The 'num' portion of an 'entity name' */
  282. seq_printf(s, "global_id %llu\n", ac->global_id);
  283. /* The -o name mount argument */
  284. seq_printf(s, "name \"%s\"\n", opt->name ? opt->name : "");
  285. /* The list of MDS session rank+state */
  286. for (mds = 0; mds < mdsc->max_sessions; mds++) {
  287. struct ceph_mds_session *session =
  288. __ceph_lookup_mds_session(mdsc, mds);
  289. if (!session) {
  290. continue;
  291. }
  292. mutex_unlock(&mdsc->mutex);
  293. seq_printf(s, "mds.%d %s\n",
  294. session->s_mds,
  295. ceph_session_state_name(session->s_state));
  296. ceph_put_mds_session(session);
  297. mutex_lock(&mdsc->mutex);
  298. }
  299. mutex_unlock(&mdsc->mutex);
  300. return 0;
  301. }
  302. static int status_show(struct seq_file *s, void *p)
  303. {
  304. struct ceph_fs_client *fsc = s->private;
  305. struct ceph_entity_inst *inst = &fsc->client->msgr.inst;
  306. struct ceph_entity_addr *client_addr = ceph_client_addr(fsc->client);
  307. seq_printf(s, "instance: %s.%lld %s/%u\n", ENTITY_NAME(inst->name),
  308. ceph_pr_addr(client_addr), le32_to_cpu(client_addr->nonce));
  309. seq_printf(s, "blocklisted: %s\n", fsc->blocklisted ? "true" : "false");
  310. return 0;
  311. }
  312. DEFINE_SHOW_ATTRIBUTE(mdsmap);
  313. DEFINE_SHOW_ATTRIBUTE(mdsc);
  314. DEFINE_SHOW_ATTRIBUTE(caps);
  315. DEFINE_SHOW_ATTRIBUTE(mds_sessions);
  316. DEFINE_SHOW_ATTRIBUTE(status);
  317. DEFINE_SHOW_ATTRIBUTE(metrics_file);
  318. DEFINE_SHOW_ATTRIBUTE(metrics_latency);
  319. DEFINE_SHOW_ATTRIBUTE(metrics_size);
  320. DEFINE_SHOW_ATTRIBUTE(metrics_caps);
  321. /*
  322. * debugfs
  323. */
  324. static int congestion_kb_set(void *data, u64 val)
  325. {
  326. struct ceph_fs_client *fsc = (struct ceph_fs_client *)data;
  327. fsc->mount_options->congestion_kb = (int)val;
  328. return 0;
  329. }
  330. static int congestion_kb_get(void *data, u64 *val)
  331. {
  332. struct ceph_fs_client *fsc = (struct ceph_fs_client *)data;
  333. *val = (u64)fsc->mount_options->congestion_kb;
  334. return 0;
  335. }
  336. DEFINE_SIMPLE_ATTRIBUTE(congestion_kb_fops, congestion_kb_get,
  337. congestion_kb_set, "%llu\n");
  338. void ceph_fs_debugfs_cleanup(struct ceph_fs_client *fsc)
  339. {
  340. dout("ceph_fs_debugfs_cleanup\n");
  341. debugfs_remove(fsc->debugfs_bdi);
  342. debugfs_remove(fsc->debugfs_congestion_kb);
  343. debugfs_remove(fsc->debugfs_mdsmap);
  344. debugfs_remove(fsc->debugfs_mds_sessions);
  345. debugfs_remove(fsc->debugfs_caps);
  346. debugfs_remove(fsc->debugfs_status);
  347. debugfs_remove(fsc->debugfs_mdsc);
  348. debugfs_remove_recursive(fsc->debugfs_metrics_dir);
  349. }
  350. void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
  351. {
  352. char name[100];
  353. dout("ceph_fs_debugfs_init\n");
  354. fsc->debugfs_congestion_kb =
  355. debugfs_create_file("writeback_congestion_kb",
  356. 0600,
  357. fsc->client->debugfs_dir,
  358. fsc,
  359. &congestion_kb_fops);
  360. snprintf(name, sizeof(name), "../../bdi/%s",
  361. bdi_dev_name(fsc->sb->s_bdi));
  362. fsc->debugfs_bdi =
  363. debugfs_create_symlink("bdi",
  364. fsc->client->debugfs_dir,
  365. name);
  366. fsc->debugfs_mdsmap = debugfs_create_file("mdsmap",
  367. 0400,
  368. fsc->client->debugfs_dir,
  369. fsc,
  370. &mdsmap_fops);
  371. fsc->debugfs_mds_sessions = debugfs_create_file("mds_sessions",
  372. 0400,
  373. fsc->client->debugfs_dir,
  374. fsc,
  375. &mds_sessions_fops);
  376. fsc->debugfs_mdsc = debugfs_create_file("mdsc",
  377. 0400,
  378. fsc->client->debugfs_dir,
  379. fsc,
  380. &mdsc_fops);
  381. fsc->debugfs_caps = debugfs_create_file("caps",
  382. 0400,
  383. fsc->client->debugfs_dir,
  384. fsc,
  385. &caps_fops);
  386. fsc->debugfs_status = debugfs_create_file("status",
  387. 0400,
  388. fsc->client->debugfs_dir,
  389. fsc,
  390. &status_fops);
  391. fsc->debugfs_metrics_dir = debugfs_create_dir("metrics",
  392. fsc->client->debugfs_dir);
  393. debugfs_create_file("file", 0400, fsc->debugfs_metrics_dir, fsc,
  394. &metrics_file_fops);
  395. debugfs_create_file("latency", 0400, fsc->debugfs_metrics_dir, fsc,
  396. &metrics_latency_fops);
  397. debugfs_create_file("size", 0400, fsc->debugfs_metrics_dir, fsc,
  398. &metrics_size_fops);
  399. debugfs_create_file("caps", 0400, fsc->debugfs_metrics_dir, fsc,
  400. &metrics_caps_fops);
  401. }
  402. #else /* CONFIG_DEBUG_FS */
  403. void ceph_fs_debugfs_init(struct ceph_fs_client *fsc)
  404. {
  405. }
  406. void ceph_fs_debugfs_cleanup(struct ceph_fs_client *fsc)
  407. {
  408. }
  409. #endif /* CONFIG_DEBUG_FS */