page_owner_sort.c 20 KB

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