page_owner_sort.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * User-space helper to sort the output of /sys/kernel/debug/page_owner
  4. *
  5. * Example use:
  6. * cat /sys/kernel/debug/page_owner > page_owner_full.txt
  7. * ./page_owner_sort page_owner_full.txt sorted_page_owner.txt
  8. * Or sort by total memory:
  9. * ./page_owner_sort -m page_owner_full.txt sorted_page_owner.txt
  10. *
  11. * See Documentation/mm/page_owner.rst
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <regex.h>
  22. #include <errno.h>
  23. #include <linux/types.h>
  24. #include <getopt.h>
  25. #define TASK_COMM_LEN 16
  26. struct block_list {
  27. char *txt;
  28. char *comm; // task command name
  29. char *stacktrace;
  30. __u64 ts_nsec;
  31. int len;
  32. int num;
  33. int page_num;
  34. pid_t pid;
  35. pid_t tgid;
  36. int allocator;
  37. };
  38. enum FILTER_BIT {
  39. FILTER_PID = 1<<1,
  40. FILTER_TGID = 1<<2,
  41. FILTER_COMM = 1<<3
  42. };
  43. enum CULL_BIT {
  44. CULL_PID = 1<<1,
  45. CULL_TGID = 1<<2,
  46. CULL_COMM = 1<<3,
  47. CULL_STACKTRACE = 1<<4,
  48. CULL_ALLOCATOR = 1<<5
  49. };
  50. enum ALLOCATOR_BIT {
  51. ALLOCATOR_CMA = 1<<1,
  52. ALLOCATOR_SLAB = 1<<2,
  53. ALLOCATOR_VMALLOC = 1<<3,
  54. ALLOCATOR_OTHERS = 1<<4
  55. };
  56. enum ARG_TYPE {
  57. ARG_TXT, ARG_COMM, ARG_STACKTRACE, ARG_ALLOC_TS, ARG_CULL_TIME,
  58. ARG_PAGE_NUM, ARG_PID, ARG_TGID, ARG_UNKNOWN, ARG_ALLOCATOR
  59. };
  60. enum SORT_ORDER {
  61. SORT_ASC = 1,
  62. SORT_DESC = -1,
  63. };
  64. enum COMP_FLAG {
  65. COMP_NO_FLAG = 0,
  66. COMP_ALLOC = 1<<0,
  67. COMP_PAGE_NUM = 1<<1,
  68. COMP_PID = 1<<2,
  69. COMP_STACK = 1<<3,
  70. COMP_NUM = 1<<4,
  71. COMP_TGID = 1<<5,
  72. COMP_COMM = 1<<6
  73. };
  74. struct filter_condition {
  75. pid_t *pids;
  76. pid_t *tgids;
  77. char **comms;
  78. int pids_size;
  79. int tgids_size;
  80. int comms_size;
  81. };
  82. struct sort_condition {
  83. int (**cmps)(const void *, const void *);
  84. int *signs;
  85. int size;
  86. };
  87. static struct filter_condition fc;
  88. static struct sort_condition sc;
  89. static regex_t order_pattern;
  90. static regex_t pid_pattern;
  91. static regex_t tgid_pattern;
  92. static regex_t comm_pattern;
  93. static regex_t ts_nsec_pattern;
  94. static struct block_list *list;
  95. static int list_size;
  96. static int max_size;
  97. static int cull;
  98. static int filter;
  99. static bool debug_on;
  100. static void set_single_cmp(int (*cmp)(const void *, const void *), int sign);
  101. int read_block(char *buf, char *ext_buf, int buf_size, FILE *fin)
  102. {
  103. char *curr = buf, *const buf_end = buf + buf_size;
  104. while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
  105. if (*curr == '\n') { /* empty line */
  106. return curr - buf;
  107. }
  108. if (!strncmp(curr, "PFN", 3)) {
  109. strcpy(ext_buf, curr);
  110. continue;
  111. }
  112. curr += strlen(curr);
  113. }
  114. return -1; /* EOF or no space left in buf. */
  115. }
  116. static int compare_txt(const void *p1, const void *p2)
  117. {
  118. const struct block_list *l1 = p1, *l2 = p2;
  119. return strcmp(l1->txt, l2->txt);
  120. }
  121. static int compare_stacktrace(const void *p1, const void *p2)
  122. {
  123. const struct block_list *l1 = p1, *l2 = p2;
  124. return strcmp(l1->stacktrace, l2->stacktrace);
  125. }
  126. static int compare_num(const void *p1, const void *p2)
  127. {
  128. const struct block_list *l1 = p1, *l2 = p2;
  129. return l1->num - l2->num;
  130. }
  131. static int compare_page_num(const void *p1, const void *p2)
  132. {
  133. const struct block_list *l1 = p1, *l2 = p2;
  134. return l1->page_num - l2->page_num;
  135. }
  136. static int compare_pid(const void *p1, const void *p2)
  137. {
  138. const struct block_list *l1 = p1, *l2 = p2;
  139. return l1->pid - l2->pid;
  140. }
  141. static int compare_tgid(const void *p1, const void *p2)
  142. {
  143. const struct block_list *l1 = p1, *l2 = p2;
  144. return l1->tgid - l2->tgid;
  145. }
  146. static int compare_allocator(const void *p1, const void *p2)
  147. {
  148. const struct block_list *l1 = p1, *l2 = p2;
  149. return l1->allocator - l2->allocator;
  150. }
  151. static int compare_comm(const void *p1, const void *p2)
  152. {
  153. const struct block_list *l1 = p1, *l2 = p2;
  154. return strcmp(l1->comm, l2->comm);
  155. }
  156. static int compare_ts(const void *p1, const void *p2)
  157. {
  158. const struct block_list *l1 = p1, *l2 = p2;
  159. if (l1->ts_nsec < l2->ts_nsec)
  160. return -1;
  161. if (l1->ts_nsec > l2->ts_nsec)
  162. return 1;
  163. return 0;
  164. }
  165. static int compare_cull_condition(const void *p1, const void *p2)
  166. {
  167. if (cull == 0)
  168. return compare_txt(p1, p2);
  169. if ((cull & CULL_STACKTRACE) && compare_stacktrace(p1, p2))
  170. return compare_stacktrace(p1, p2);
  171. if ((cull & CULL_PID) && compare_pid(p1, p2))
  172. return compare_pid(p1, p2);
  173. if ((cull & CULL_TGID) && compare_tgid(p1, p2))
  174. return compare_tgid(p1, p2);
  175. if ((cull & CULL_COMM) && compare_comm(p1, p2))
  176. return compare_comm(p1, p2);
  177. if ((cull & CULL_ALLOCATOR) && compare_allocator(p1, p2))
  178. return compare_allocator(p1, p2);
  179. return 0;
  180. }
  181. static int compare_sort_condition(const void *p1, const void *p2)
  182. {
  183. int cmp = 0;
  184. for (int i = 0; i < sc.size; ++i)
  185. if (cmp == 0)
  186. cmp = sc.signs[i] * sc.cmps[i](p1, p2);
  187. return cmp;
  188. }
  189. static int remove_pattern(regex_t *pattern, char *buf, int len)
  190. {
  191. regmatch_t pmatch[2];
  192. int err;
  193. err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
  194. if (err != 0 || pmatch[1].rm_so == -1)
  195. return len;
  196. memcpy(buf + pmatch[1].rm_so,
  197. buf + pmatch[1].rm_eo, len - pmatch[1].rm_eo);
  198. return len - (pmatch[1].rm_eo - pmatch[1].rm_so);
  199. }
  200. static int search_pattern(regex_t *pattern, char *pattern_str, char *buf)
  201. {
  202. int err, val_len;
  203. regmatch_t pmatch[2];
  204. err = regexec(pattern, buf, 2, pmatch, REG_NOTBOL);
  205. if (err != 0 || pmatch[1].rm_so == -1) {
  206. if (debug_on)
  207. fprintf(stderr, "no matching pattern in %s\n", buf);
  208. return -1;
  209. }
  210. val_len = pmatch[1].rm_eo - pmatch[1].rm_so;
  211. memcpy(pattern_str, buf + pmatch[1].rm_so, val_len);
  212. return 0;
  213. }
  214. static bool check_regcomp(regex_t *pattern, const char *regex)
  215. {
  216. int err;
  217. err = regcomp(pattern, regex, REG_EXTENDED | REG_NEWLINE);
  218. if (err != 0 || pattern->re_nsub != 1) {
  219. fprintf(stderr, "Invalid pattern %s code %d\n", regex, err);
  220. return false;
  221. }
  222. return true;
  223. }
  224. static char **explode(char sep, const char *str, int *size)
  225. {
  226. int count = 0, len = strlen(str);
  227. int lastindex = -1, j = 0;
  228. for (int i = 0; i < len; i++)
  229. if (str[i] == sep)
  230. count++;
  231. char **ret = calloc(++count, sizeof(char *));
  232. for (int i = 0; i < len; i++) {
  233. if (str[i] == sep) {
  234. ret[j] = calloc(i - lastindex, sizeof(char));
  235. memcpy(ret[j++], str + lastindex + 1, i - lastindex - 1);
  236. lastindex = i;
  237. }
  238. }
  239. if (lastindex <= len - 1) {
  240. ret[j] = calloc(len - lastindex, sizeof(char));
  241. memcpy(ret[j++], str + lastindex + 1, strlen(str) - 1 - lastindex);
  242. }
  243. *size = j;
  244. return ret;
  245. }
  246. static void free_explode(char **arr, int size)
  247. {
  248. for (int i = 0; i < size; i++)
  249. free(arr[i]);
  250. free(arr);
  251. }
  252. # define FIELD_BUFF 25
  253. static int get_page_num(char *buf)
  254. {
  255. int order_val;
  256. char order_str[FIELD_BUFF] = {0};
  257. char *endptr;
  258. search_pattern(&order_pattern, order_str, buf);
  259. errno = 0;
  260. order_val = strtol(order_str, &endptr, 10);
  261. if (order_val > 64 || errno != 0 || endptr == order_str || *endptr != '\0') {
  262. if (debug_on)
  263. fprintf(stderr, "wrong order in follow buf:\n%s\n", buf);
  264. return 0;
  265. }
  266. return 1 << order_val;
  267. }
  268. static pid_t get_pid(char *buf)
  269. {
  270. pid_t pid;
  271. char pid_str[FIELD_BUFF] = {0};
  272. char *endptr;
  273. search_pattern(&pid_pattern, pid_str, buf);
  274. errno = 0;
  275. pid = strtol(pid_str, &endptr, 10);
  276. if (errno != 0 || endptr == pid_str || *endptr != '\0') {
  277. if (debug_on)
  278. fprintf(stderr, "wrong/invalid pid in follow buf:\n%s\n", buf);
  279. return -1;
  280. }
  281. return pid;
  282. }
  283. static pid_t get_tgid(char *buf)
  284. {
  285. pid_t tgid;
  286. char tgid_str[FIELD_BUFF] = {0};
  287. char *endptr;
  288. search_pattern(&tgid_pattern, tgid_str, buf);
  289. errno = 0;
  290. tgid = strtol(tgid_str, &endptr, 10);
  291. if (errno != 0 || endptr == tgid_str || *endptr != '\0') {
  292. if (debug_on)
  293. fprintf(stderr, "wrong/invalid tgid in follow buf:\n%s\n", buf);
  294. return -1;
  295. }
  296. return tgid;
  297. }
  298. static __u64 get_ts_nsec(char *buf)
  299. {
  300. __u64 ts_nsec;
  301. char ts_nsec_str[FIELD_BUFF] = {0};
  302. char *endptr;
  303. search_pattern(&ts_nsec_pattern, ts_nsec_str, buf);
  304. errno = 0;
  305. ts_nsec = strtoull(ts_nsec_str, &endptr, 10);
  306. if (errno != 0 || endptr == ts_nsec_str || *endptr != '\0') {
  307. if (debug_on)
  308. fprintf(stderr, "wrong ts_nsec in follow buf:\n%s\n", buf);
  309. return -1;
  310. }
  311. return ts_nsec;
  312. }
  313. static char *get_comm(char *buf)
  314. {
  315. char *comm_str = malloc(TASK_COMM_LEN);
  316. memset(comm_str, 0, TASK_COMM_LEN);
  317. search_pattern(&comm_pattern, comm_str, buf);
  318. errno = 0;
  319. if (errno != 0) {
  320. if (debug_on)
  321. fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf);
  322. free(comm_str);
  323. return NULL;
  324. }
  325. return comm_str;
  326. }
  327. static int get_arg_type(const char *arg)
  328. {
  329. if (!strcmp(arg, "pid") || !strcmp(arg, "p"))
  330. return ARG_PID;
  331. else if (!strcmp(arg, "tgid") || !strcmp(arg, "tg"))
  332. return ARG_TGID;
  333. else if (!strcmp(arg, "name") || !strcmp(arg, "n"))
  334. return ARG_COMM;
  335. else if (!strcmp(arg, "stacktrace") || !strcmp(arg, "st"))
  336. return ARG_STACKTRACE;
  337. else if (!strcmp(arg, "txt") || !strcmp(arg, "T"))
  338. return ARG_TXT;
  339. else if (!strcmp(arg, "alloc_ts") || !strcmp(arg, "at"))
  340. return ARG_ALLOC_TS;
  341. else if (!strcmp(arg, "allocator") || !strcmp(arg, "ator"))
  342. return ARG_ALLOCATOR;
  343. else {
  344. return ARG_UNKNOWN;
  345. }
  346. }
  347. static int get_allocator(const char *buf, const char *migrate_info)
  348. {
  349. char *tmp, *first_line, *second_line;
  350. int allocator = 0;
  351. if (strstr(migrate_info, "CMA"))
  352. allocator |= ALLOCATOR_CMA;
  353. if (strstr(migrate_info, "slab"))
  354. allocator |= ALLOCATOR_SLAB;
  355. tmp = strstr(buf, "__vmalloc_node_range");
  356. if (tmp) {
  357. second_line = tmp;
  358. while (*tmp != '\n')
  359. tmp--;
  360. tmp--;
  361. while (*tmp != '\n')
  362. tmp--;
  363. first_line = ++tmp;
  364. tmp = strstr(tmp, "alloc_pages");
  365. if (tmp && first_line <= tmp && tmp < second_line)
  366. allocator |= ALLOCATOR_VMALLOC;
  367. }
  368. if (allocator == 0)
  369. allocator = ALLOCATOR_OTHERS;
  370. return allocator;
  371. }
  372. static bool match_num_list(int num, int *list, int list_size)
  373. {
  374. for (int i = 0; i < list_size; ++i)
  375. if (list[i] == num)
  376. return true;
  377. return false;
  378. }
  379. static bool match_str_list(const char *str, char **list, int list_size)
  380. {
  381. for (int i = 0; i < list_size; ++i)
  382. if (!strcmp(list[i], str))
  383. return true;
  384. return false;
  385. }
  386. static bool is_need(char *buf)
  387. {
  388. if ((filter & FILTER_PID) && !match_num_list(get_pid(buf), fc.pids, fc.pids_size))
  389. return false;
  390. if ((filter & FILTER_TGID) &&
  391. !match_num_list(get_tgid(buf), fc.tgids, fc.tgids_size))
  392. return false;
  393. char *comm = get_comm(buf);
  394. if ((filter & FILTER_COMM) &&
  395. !match_str_list(comm, fc.comms, fc.comms_size)) {
  396. free(comm);
  397. return false;
  398. }
  399. free(comm);
  400. return true;
  401. }
  402. static bool add_list(char *buf, int len, char *ext_buf)
  403. {
  404. if (list_size == max_size) {
  405. fprintf(stderr, "max_size too small??\n");
  406. return false;
  407. }
  408. if (!is_need(buf))
  409. return true;
  410. list[list_size].pid = get_pid(buf);
  411. list[list_size].tgid = get_tgid(buf);
  412. list[list_size].comm = get_comm(buf);
  413. list[list_size].txt = malloc(len+1);
  414. if (!list[list_size].txt) {
  415. fprintf(stderr, "Out of memory\n");
  416. return false;
  417. }
  418. memcpy(list[list_size].txt, buf, len);
  419. if (sc.cmps[0] != compare_ts) {
  420. len = remove_pattern(&ts_nsec_pattern, list[list_size].txt, len);
  421. }
  422. list[list_size].txt[len] = 0;
  423. list[list_size].len = len;
  424. list[list_size].num = 1;
  425. list[list_size].page_num = get_page_num(buf);
  426. list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
  427. if (*list[list_size].stacktrace == '\n')
  428. list[list_size].stacktrace++;
  429. list[list_size].ts_nsec = get_ts_nsec(buf);
  430. list[list_size].allocator = get_allocator(buf, ext_buf);
  431. list_size++;
  432. if (list_size % 1000 == 0) {
  433. printf("loaded %d\r", list_size);
  434. fflush(stdout);
  435. }
  436. return true;
  437. }
  438. static bool parse_cull_args(const char *arg_str)
  439. {
  440. int size = 0;
  441. char **args = explode(',', arg_str, &size);
  442. for (int i = 0; i < size; ++i) {
  443. int arg_type = get_arg_type(args[i]);
  444. if (arg_type == ARG_PID)
  445. cull |= CULL_PID;
  446. else if (arg_type == ARG_TGID)
  447. cull |= CULL_TGID;
  448. else if (arg_type == ARG_COMM)
  449. cull |= CULL_COMM;
  450. else if (arg_type == ARG_STACKTRACE)
  451. cull |= CULL_STACKTRACE;
  452. else if (arg_type == ARG_ALLOCATOR)
  453. cull |= CULL_ALLOCATOR;
  454. else {
  455. free_explode(args, size);
  456. return false;
  457. }
  458. }
  459. free_explode(args, size);
  460. if (sc.size == 0)
  461. set_single_cmp(compare_num, SORT_DESC);
  462. return true;
  463. }
  464. static void set_single_cmp(int (*cmp)(const void *, const void *), int sign)
  465. {
  466. if (sc.signs == NULL || sc.size < 1)
  467. sc.signs = calloc(1, sizeof(int));
  468. sc.signs[0] = sign;
  469. if (sc.cmps == NULL || sc.size < 1)
  470. sc.cmps = calloc(1, sizeof(int *));
  471. sc.cmps[0] = cmp;
  472. sc.size = 1;
  473. }
  474. static bool parse_sort_args(const char *arg_str)
  475. {
  476. int size = 0;
  477. if (sc.size != 0) { /* reset sort_condition */
  478. free(sc.signs);
  479. free(sc.cmps);
  480. size = 0;
  481. }
  482. char **args = explode(',', arg_str, &size);
  483. sc.signs = calloc(size, sizeof(int));
  484. sc.cmps = calloc(size, sizeof(int *));
  485. for (int i = 0; i < size; ++i) {
  486. int offset = 0;
  487. sc.signs[i] = SORT_ASC;
  488. if (args[i][0] == '-' || args[i][0] == '+') {
  489. if (args[i][0] == '-')
  490. sc.signs[i] = SORT_DESC;
  491. offset = 1;
  492. }
  493. int arg_type = get_arg_type(args[i]+offset);
  494. if (arg_type == ARG_PID)
  495. sc.cmps[i] = compare_pid;
  496. else if (arg_type == ARG_TGID)
  497. sc.cmps[i] = compare_tgid;
  498. else if (arg_type == ARG_COMM)
  499. sc.cmps[i] = compare_comm;
  500. else if (arg_type == ARG_STACKTRACE)
  501. sc.cmps[i] = compare_stacktrace;
  502. else if (arg_type == ARG_ALLOC_TS)
  503. sc.cmps[i] = compare_ts;
  504. else if (arg_type == ARG_TXT)
  505. sc.cmps[i] = compare_txt;
  506. else if (arg_type == ARG_ALLOCATOR)
  507. sc.cmps[i] = compare_allocator;
  508. else {
  509. free_explode(args, size);
  510. sc.size = 0;
  511. return false;
  512. }
  513. }
  514. sc.size = size;
  515. free_explode(args, size);
  516. return true;
  517. }
  518. static int *parse_nums_list(char *arg_str, int *list_size)
  519. {
  520. int size = 0;
  521. char **args = explode(',', arg_str, &size);
  522. int *list = calloc(size, sizeof(int));
  523. errno = 0;
  524. for (int i = 0; i < size; ++i) {
  525. char *endptr = NULL;
  526. list[i] = strtol(args[i], &endptr, 10);
  527. if (errno != 0 || endptr == args[i] || *endptr != '\0') {
  528. free(list);
  529. return NULL;
  530. }
  531. }
  532. *list_size = size;
  533. free_explode(args, size);
  534. return list;
  535. }
  536. static void print_allocator(FILE *out, int allocator)
  537. {
  538. fprintf(out, "allocated by ");
  539. if (allocator & ALLOCATOR_CMA)
  540. fprintf(out, "CMA ");
  541. if (allocator & ALLOCATOR_SLAB)
  542. fprintf(out, "SLAB ");
  543. if (allocator & ALLOCATOR_VMALLOC)
  544. fprintf(out, "VMALLOC ");
  545. if (allocator & ALLOCATOR_OTHERS)
  546. fprintf(out, "OTHERS ");
  547. }
  548. #define BUF_SIZE (128 * 1024)
  549. static void usage(void)
  550. {
  551. printf("Usage: ./page_owner_sort [OPTIONS] <input> <output>\n"
  552. "-a\t\t\tSort by memory allocation time.\n"
  553. "-m\t\t\tSort by total memory.\n"
  554. "-n\t\t\tSort by task command name.\n"
  555. "-p\t\t\tSort by pid.\n"
  556. "-P\t\t\tSort by tgid.\n"
  557. "-s\t\t\tSort by the stacktrace.\n"
  558. "-t\t\t\tSort by number of times record is seen (default).\n\n"
  559. "--pid <pidlist>\t\tSelect by pid. This selects the information"
  560. " of\n\t\t\tblocks whose process ID numbers appear in <pidlist>.\n"
  561. "--tgid <tgidlist>\tSelect by tgid. This selects the information"
  562. " of\n\t\t\tblocks whose Thread Group ID numbers appear in "
  563. "<tgidlist>.\n"
  564. "--name <cmdlist>\tSelect by command name. This selects the"
  565. " information\n\t\t\tof blocks whose command name appears in"
  566. " <cmdlist>.\n"
  567. "--cull <rules>\t\tCull by user-defined rules. <rules> is a "
  568. "single\n\t\t\targument in the form of a comma-separated list "
  569. "with some\n\t\t\tcommon fields predefined (pid, tgid, comm, "
  570. "stacktrace, allocator)\n"
  571. "--sort <order>\t\tSpecify sort order as: [+|-]key[,[+|-]key[,...]]\n"
  572. );
  573. }
  574. int main(int argc, char **argv)
  575. {
  576. FILE *fin, *fout;
  577. char *buf, *ext_buf;
  578. int i, count, compare_flag;
  579. struct stat st;
  580. int opt;
  581. struct option longopts[] = {
  582. { "pid", required_argument, NULL, 1 },
  583. { "tgid", required_argument, NULL, 2 },
  584. { "name", required_argument, NULL, 3 },
  585. { "cull", required_argument, NULL, 4 },
  586. { "sort", required_argument, NULL, 5 },
  587. { "help", no_argument, NULL, 'h' },
  588. { 0, 0, 0, 0},
  589. };
  590. compare_flag = COMP_NO_FLAG;
  591. while ((opt = getopt_long(argc, argv, "admnpstPh", longopts, NULL)) != -1)
  592. switch (opt) {
  593. case 'a':
  594. compare_flag |= COMP_ALLOC;
  595. break;
  596. case 'd':
  597. debug_on = true;
  598. break;
  599. case 'm':
  600. compare_flag |= COMP_PAGE_NUM;
  601. break;
  602. case 'p':
  603. compare_flag |= COMP_PID;
  604. break;
  605. case 's':
  606. compare_flag |= COMP_STACK;
  607. break;
  608. case 't':
  609. compare_flag |= COMP_NUM;
  610. break;
  611. case 'P':
  612. compare_flag |= COMP_TGID;
  613. break;
  614. case 'n':
  615. compare_flag |= COMP_COMM;
  616. break;
  617. case 'h':
  618. usage();
  619. exit(0);
  620. case 1:
  621. filter = filter | FILTER_PID;
  622. fc.pids = parse_nums_list(optarg, &fc.pids_size);
  623. if (fc.pids == NULL) {
  624. fprintf(stderr, "wrong/invalid pid in from the command line:%s\n",
  625. optarg);
  626. exit(1);
  627. }
  628. break;
  629. case 2:
  630. filter = filter | FILTER_TGID;
  631. fc.tgids = parse_nums_list(optarg, &fc.tgids_size);
  632. if (fc.tgids == NULL) {
  633. fprintf(stderr, "wrong/invalid tgid in from the command line:%s\n",
  634. optarg);
  635. exit(1);
  636. }
  637. break;
  638. case 3:
  639. filter = filter | FILTER_COMM;
  640. fc.comms = explode(',', optarg, &fc.comms_size);
  641. break;
  642. case 4:
  643. if (!parse_cull_args(optarg)) {
  644. fprintf(stderr, "wrong argument after --cull option:%s\n",
  645. optarg);
  646. exit(1);
  647. }
  648. break;
  649. case 5:
  650. if (!parse_sort_args(optarg)) {
  651. fprintf(stderr, "wrong argument after --sort option:%s\n",
  652. optarg);
  653. exit(1);
  654. }
  655. break;
  656. default:
  657. usage();
  658. exit(1);
  659. }
  660. if (optind >= (argc - 1)) {
  661. usage();
  662. exit(1);
  663. }
  664. /* Only one compare option is allowed, yet we also want handle the
  665. * default case were no option is provided, but we still want to
  666. * match the behavior of the -t option (compare by number of times
  667. * a record is seen
  668. */
  669. switch (compare_flag) {
  670. case COMP_ALLOC:
  671. set_single_cmp(compare_ts, SORT_ASC);
  672. break;
  673. case COMP_PAGE_NUM:
  674. set_single_cmp(compare_page_num, SORT_DESC);
  675. break;
  676. case COMP_PID:
  677. set_single_cmp(compare_pid, SORT_ASC);
  678. break;
  679. case COMP_STACK:
  680. set_single_cmp(compare_stacktrace, SORT_ASC);
  681. break;
  682. case COMP_NO_FLAG:
  683. case COMP_NUM:
  684. set_single_cmp(compare_num, SORT_DESC);
  685. break;
  686. case COMP_TGID:
  687. set_single_cmp(compare_tgid, SORT_ASC);
  688. break;
  689. case COMP_COMM:
  690. set_single_cmp(compare_comm, SORT_ASC);
  691. break;
  692. default:
  693. usage();
  694. exit(1);
  695. }
  696. fin = fopen(argv[optind], "r");
  697. fout = fopen(argv[optind + 1], "w");
  698. if (!fin || !fout) {
  699. usage();
  700. perror("open: ");
  701. exit(1);
  702. }
  703. if (!check_regcomp(&order_pattern, "order\\s*([0-9]*),"))
  704. goto out_order;
  705. if (!check_regcomp(&pid_pattern, "pid\\s*([0-9]*),"))
  706. goto out_pid;
  707. if (!check_regcomp(&tgid_pattern, "tgid\\s*([0-9]*) "))
  708. goto out_tgid;
  709. if (!check_regcomp(&comm_pattern, "tgid\\s*[0-9]*\\s*\\((.*)\\),\\s*ts"))
  710. goto out_comm;
  711. if (!check_regcomp(&ts_nsec_pattern, "ts\\s*([0-9]*)\\s*ns"))
  712. goto out_ts;
  713. fstat(fileno(fin), &st);
  714. max_size = st.st_size / 100; /* hack ... */
  715. list = malloc(max_size * sizeof(*list));
  716. buf = malloc(BUF_SIZE);
  717. ext_buf = malloc(BUF_SIZE);
  718. if (!list || !buf || !ext_buf) {
  719. fprintf(stderr, "Out of memory\n");
  720. goto out_free;
  721. }
  722. for ( ; ; ) {
  723. int buf_len = read_block(buf, ext_buf, BUF_SIZE, fin);
  724. if (buf_len < 0)
  725. break;
  726. if (!add_list(buf, buf_len, ext_buf))
  727. goto out_free;
  728. }
  729. printf("loaded %d\n", list_size);
  730. printf("sorting ....\n");
  731. qsort(list, list_size, sizeof(list[0]), compare_cull_condition);
  732. printf("culling\n");
  733. for (i = count = 0; i < list_size; i++) {
  734. if (count == 0 ||
  735. compare_cull_condition((void *)(&list[count-1]), (void *)(&list[i])) != 0) {
  736. list[count++] = list[i];
  737. } else {
  738. list[count-1].num += list[i].num;
  739. list[count-1].page_num += list[i].page_num;
  740. }
  741. }
  742. qsort(list, count, sizeof(list[0]), compare_sort_condition);
  743. for (i = 0; i < count; i++) {
  744. if (cull == 0) {
  745. fprintf(fout, "%d times, %d pages, ", list[i].num, list[i].page_num);
  746. print_allocator(fout, list[i].allocator);
  747. fprintf(fout, ":\n%s\n", list[i].txt);
  748. }
  749. else {
  750. fprintf(fout, "%d times, %d pages",
  751. list[i].num, list[i].page_num);
  752. if (cull & CULL_PID || filter & FILTER_PID)
  753. fprintf(fout, ", PID %d", list[i].pid);
  754. if (cull & CULL_TGID || filter & FILTER_TGID)
  755. fprintf(fout, ", TGID %d", list[i].tgid);
  756. if (cull & CULL_COMM || filter & FILTER_COMM)
  757. fprintf(fout, ", task_comm_name: %s", list[i].comm);
  758. if (cull & CULL_ALLOCATOR) {
  759. fprintf(fout, ", ");
  760. print_allocator(fout, list[i].allocator);
  761. }
  762. if (cull & CULL_STACKTRACE)
  763. fprintf(fout, ":\n%s", list[i].stacktrace);
  764. fprintf(fout, "\n");
  765. }
  766. }
  767. out_free:
  768. if (ext_buf)
  769. free(ext_buf);
  770. if (buf)
  771. free(buf);
  772. if (list)
  773. free(list);
  774. out_ts:
  775. regfree(&ts_nsec_pattern);
  776. out_comm:
  777. regfree(&comm_pattern);
  778. out_tgid:
  779. regfree(&tgid_pattern);
  780. out_pid:
  781. regfree(&pid_pattern);
  782. out_order:
  783. regfree(&order_pattern);
  784. return 0;
  785. }