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