trace_events_hist.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. /*
  2. * trace_events_hist - trace event hist triggers
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. #include <linux/stacktrace.h>
  21. #include "tracing_map.h"
  22. #include "trace.h"
  23. struct hist_field;
  24. typedef u64 (*hist_field_fn_t) (struct hist_field *field, void *event);
  25. struct hist_field {
  26. struct ftrace_event_field *field;
  27. unsigned long flags;
  28. hist_field_fn_t fn;
  29. unsigned int size;
  30. unsigned int offset;
  31. };
  32. static u64 hist_field_none(struct hist_field *field, void *event)
  33. {
  34. return 0;
  35. }
  36. static u64 hist_field_counter(struct hist_field *field, void *event)
  37. {
  38. return 1;
  39. }
  40. static u64 hist_field_string(struct hist_field *hist_field, void *event)
  41. {
  42. char *addr = (char *)(event + hist_field->field->offset);
  43. return (u64)(unsigned long)addr;
  44. }
  45. static u64 hist_field_dynstring(struct hist_field *hist_field, void *event)
  46. {
  47. u32 str_item = *(u32 *)(event + hist_field->field->offset);
  48. int str_loc = str_item & 0xffff;
  49. char *addr = (char *)(event + str_loc);
  50. return (u64)(unsigned long)addr;
  51. }
  52. static u64 hist_field_pstring(struct hist_field *hist_field, void *event)
  53. {
  54. char **addr = (char **)(event + hist_field->field->offset);
  55. return (u64)(unsigned long)*addr;
  56. }
  57. static u64 hist_field_log2(struct hist_field *hist_field, void *event)
  58. {
  59. u64 val = *(u64 *)(event + hist_field->field->offset);
  60. return (u64) ilog2(roundup_pow_of_two(val));
  61. }
  62. #define DEFINE_HIST_FIELD_FN(type) \
  63. static u64 hist_field_##type(struct hist_field *hist_field, void *event)\
  64. { \
  65. type *addr = (type *)(event + hist_field->field->offset); \
  66. \
  67. return (u64)(unsigned long)*addr; \
  68. }
  69. DEFINE_HIST_FIELD_FN(s64);
  70. DEFINE_HIST_FIELD_FN(u64);
  71. DEFINE_HIST_FIELD_FN(s32);
  72. DEFINE_HIST_FIELD_FN(u32);
  73. DEFINE_HIST_FIELD_FN(s16);
  74. DEFINE_HIST_FIELD_FN(u16);
  75. DEFINE_HIST_FIELD_FN(s8);
  76. DEFINE_HIST_FIELD_FN(u8);
  77. #define for_each_hist_field(i, hist_data) \
  78. for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
  79. #define for_each_hist_val_field(i, hist_data) \
  80. for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
  81. #define for_each_hist_key_field(i, hist_data) \
  82. for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
  83. #define HIST_STACKTRACE_DEPTH 16
  84. #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
  85. #define HIST_STACKTRACE_SKIP 5
  86. #define HITCOUNT_IDX 0
  87. #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
  88. enum hist_field_flags {
  89. HIST_FIELD_FL_HITCOUNT = 1,
  90. HIST_FIELD_FL_KEY = 2,
  91. HIST_FIELD_FL_STRING = 4,
  92. HIST_FIELD_FL_HEX = 8,
  93. HIST_FIELD_FL_SYM = 16,
  94. HIST_FIELD_FL_SYM_OFFSET = 32,
  95. HIST_FIELD_FL_EXECNAME = 64,
  96. HIST_FIELD_FL_SYSCALL = 128,
  97. HIST_FIELD_FL_STACKTRACE = 256,
  98. HIST_FIELD_FL_LOG2 = 512,
  99. };
  100. struct hist_trigger_attrs {
  101. char *keys_str;
  102. char *vals_str;
  103. char *sort_key_str;
  104. char *name;
  105. bool pause;
  106. bool cont;
  107. bool clear;
  108. unsigned int map_bits;
  109. };
  110. struct hist_trigger_data {
  111. struct hist_field *fields[TRACING_MAP_FIELDS_MAX];
  112. unsigned int n_vals;
  113. unsigned int n_keys;
  114. unsigned int n_fields;
  115. unsigned int key_size;
  116. struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
  117. unsigned int n_sort_keys;
  118. struct trace_event_file *event_file;
  119. struct hist_trigger_attrs *attrs;
  120. struct tracing_map *map;
  121. };
  122. static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
  123. {
  124. hist_field_fn_t fn = NULL;
  125. switch (field_size) {
  126. case 8:
  127. if (field_is_signed)
  128. fn = hist_field_s64;
  129. else
  130. fn = hist_field_u64;
  131. break;
  132. case 4:
  133. if (field_is_signed)
  134. fn = hist_field_s32;
  135. else
  136. fn = hist_field_u32;
  137. break;
  138. case 2:
  139. if (field_is_signed)
  140. fn = hist_field_s16;
  141. else
  142. fn = hist_field_u16;
  143. break;
  144. case 1:
  145. if (field_is_signed)
  146. fn = hist_field_s8;
  147. else
  148. fn = hist_field_u8;
  149. break;
  150. }
  151. return fn;
  152. }
  153. static int parse_map_size(char *str)
  154. {
  155. unsigned long size, map_bits;
  156. int ret;
  157. strsep(&str, "=");
  158. if (!str) {
  159. ret = -EINVAL;
  160. goto out;
  161. }
  162. ret = kstrtoul(str, 0, &size);
  163. if (ret)
  164. goto out;
  165. map_bits = ilog2(roundup_pow_of_two(size));
  166. if (map_bits < TRACING_MAP_BITS_MIN ||
  167. map_bits > TRACING_MAP_BITS_MAX)
  168. ret = -EINVAL;
  169. else
  170. ret = map_bits;
  171. out:
  172. return ret;
  173. }
  174. static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
  175. {
  176. if (!attrs)
  177. return;
  178. kfree(attrs->name);
  179. kfree(attrs->sort_key_str);
  180. kfree(attrs->keys_str);
  181. kfree(attrs->vals_str);
  182. kfree(attrs);
  183. }
  184. static struct hist_trigger_attrs *parse_hist_trigger_attrs(char *trigger_str)
  185. {
  186. struct hist_trigger_attrs *attrs;
  187. int ret = 0;
  188. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  189. if (!attrs)
  190. return ERR_PTR(-ENOMEM);
  191. while (trigger_str) {
  192. char *str = strsep(&trigger_str, ":");
  193. if ((strncmp(str, "key=", strlen("key=")) == 0) ||
  194. (strncmp(str, "keys=", strlen("keys=")) == 0))
  195. attrs->keys_str = kstrdup(str, GFP_KERNEL);
  196. else if ((strncmp(str, "val=", strlen("val=")) == 0) ||
  197. (strncmp(str, "vals=", strlen("vals=")) == 0) ||
  198. (strncmp(str, "values=", strlen("values=")) == 0))
  199. attrs->vals_str = kstrdup(str, GFP_KERNEL);
  200. else if (strncmp(str, "sort=", strlen("sort=")) == 0)
  201. attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
  202. else if (strncmp(str, "name=", strlen("name=")) == 0)
  203. attrs->name = kstrdup(str, GFP_KERNEL);
  204. else if (strcmp(str, "pause") == 0)
  205. attrs->pause = true;
  206. else if ((strcmp(str, "cont") == 0) ||
  207. (strcmp(str, "continue") == 0))
  208. attrs->cont = true;
  209. else if (strcmp(str, "clear") == 0)
  210. attrs->clear = true;
  211. else if (strncmp(str, "size=", strlen("size=")) == 0) {
  212. int map_bits = parse_map_size(str);
  213. if (map_bits < 0) {
  214. ret = map_bits;
  215. goto free;
  216. }
  217. attrs->map_bits = map_bits;
  218. } else {
  219. ret = -EINVAL;
  220. goto free;
  221. }
  222. }
  223. if (!attrs->keys_str) {
  224. ret = -EINVAL;
  225. goto free;
  226. }
  227. return attrs;
  228. free:
  229. destroy_hist_trigger_attrs(attrs);
  230. return ERR_PTR(ret);
  231. }
  232. static inline void save_comm(char *comm, struct task_struct *task)
  233. {
  234. if (!task->pid) {
  235. strcpy(comm, "<idle>");
  236. return;
  237. }
  238. if (WARN_ON_ONCE(task->pid < 0)) {
  239. strcpy(comm, "<XXX>");
  240. return;
  241. }
  242. memcpy(comm, task->comm, TASK_COMM_LEN);
  243. }
  244. static void hist_trigger_elt_comm_free(struct tracing_map_elt *elt)
  245. {
  246. kfree((char *)elt->private_data);
  247. }
  248. static int hist_trigger_elt_comm_alloc(struct tracing_map_elt *elt)
  249. {
  250. struct hist_trigger_data *hist_data = elt->map->private_data;
  251. struct hist_field *key_field;
  252. unsigned int i;
  253. for_each_hist_key_field(i, hist_data) {
  254. key_field = hist_data->fields[i];
  255. if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
  256. unsigned int size = TASK_COMM_LEN + 1;
  257. elt->private_data = kzalloc(size, GFP_KERNEL);
  258. if (!elt->private_data)
  259. return -ENOMEM;
  260. break;
  261. }
  262. }
  263. return 0;
  264. }
  265. static void hist_trigger_elt_comm_copy(struct tracing_map_elt *to,
  266. struct tracing_map_elt *from)
  267. {
  268. char *comm_from = from->private_data;
  269. char *comm_to = to->private_data;
  270. if (comm_from)
  271. memcpy(comm_to, comm_from, TASK_COMM_LEN + 1);
  272. }
  273. static void hist_trigger_elt_comm_init(struct tracing_map_elt *elt)
  274. {
  275. char *comm = elt->private_data;
  276. if (comm)
  277. save_comm(comm, current);
  278. }
  279. static const struct tracing_map_ops hist_trigger_elt_comm_ops = {
  280. .elt_alloc = hist_trigger_elt_comm_alloc,
  281. .elt_copy = hist_trigger_elt_comm_copy,
  282. .elt_free = hist_trigger_elt_comm_free,
  283. .elt_init = hist_trigger_elt_comm_init,
  284. };
  285. static void destroy_hist_field(struct hist_field *hist_field)
  286. {
  287. kfree(hist_field);
  288. }
  289. static struct hist_field *create_hist_field(struct ftrace_event_field *field,
  290. unsigned long flags)
  291. {
  292. struct hist_field *hist_field;
  293. if (field && is_function_field(field))
  294. return NULL;
  295. hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
  296. if (!hist_field)
  297. return NULL;
  298. if (flags & HIST_FIELD_FL_HITCOUNT) {
  299. hist_field->fn = hist_field_counter;
  300. goto out;
  301. }
  302. if (flags & HIST_FIELD_FL_STACKTRACE) {
  303. hist_field->fn = hist_field_none;
  304. goto out;
  305. }
  306. if (flags & HIST_FIELD_FL_LOG2) {
  307. hist_field->fn = hist_field_log2;
  308. goto out;
  309. }
  310. if (WARN_ON_ONCE(!field))
  311. goto out;
  312. if (is_string_field(field)) {
  313. flags |= HIST_FIELD_FL_STRING;
  314. if (field->filter_type == FILTER_STATIC_STRING)
  315. hist_field->fn = hist_field_string;
  316. else if (field->filter_type == FILTER_DYN_STRING)
  317. hist_field->fn = hist_field_dynstring;
  318. else
  319. hist_field->fn = hist_field_pstring;
  320. } else {
  321. hist_field->fn = select_value_fn(field->size,
  322. field->is_signed);
  323. if (!hist_field->fn) {
  324. destroy_hist_field(hist_field);
  325. return NULL;
  326. }
  327. }
  328. out:
  329. hist_field->field = field;
  330. hist_field->flags = flags;
  331. return hist_field;
  332. }
  333. static void destroy_hist_fields(struct hist_trigger_data *hist_data)
  334. {
  335. unsigned int i;
  336. for (i = 0; i < TRACING_MAP_FIELDS_MAX; i++) {
  337. if (hist_data->fields[i]) {
  338. destroy_hist_field(hist_data->fields[i]);
  339. hist_data->fields[i] = NULL;
  340. }
  341. }
  342. }
  343. static int create_hitcount_val(struct hist_trigger_data *hist_data)
  344. {
  345. hist_data->fields[HITCOUNT_IDX] =
  346. create_hist_field(NULL, HIST_FIELD_FL_HITCOUNT);
  347. if (!hist_data->fields[HITCOUNT_IDX])
  348. return -ENOMEM;
  349. hist_data->n_vals++;
  350. if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
  351. return -EINVAL;
  352. return 0;
  353. }
  354. static int create_val_field(struct hist_trigger_data *hist_data,
  355. unsigned int val_idx,
  356. struct trace_event_file *file,
  357. char *field_str)
  358. {
  359. struct ftrace_event_field *field = NULL;
  360. unsigned long flags = 0;
  361. char *field_name;
  362. int ret = 0;
  363. if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
  364. return -EINVAL;
  365. field_name = strsep(&field_str, ".");
  366. if (field_str) {
  367. if (strcmp(field_str, "hex") == 0)
  368. flags |= HIST_FIELD_FL_HEX;
  369. else {
  370. ret = -EINVAL;
  371. goto out;
  372. }
  373. }
  374. field = trace_find_event_field(file->event_call, field_name);
  375. if (!field || !field->size) {
  376. ret = -EINVAL;
  377. goto out;
  378. }
  379. hist_data->fields[val_idx] = create_hist_field(field, flags);
  380. if (!hist_data->fields[val_idx]) {
  381. ret = -ENOMEM;
  382. goto out;
  383. }
  384. ++hist_data->n_vals;
  385. if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
  386. ret = -EINVAL;
  387. out:
  388. return ret;
  389. }
  390. static int create_val_fields(struct hist_trigger_data *hist_data,
  391. struct trace_event_file *file)
  392. {
  393. char *fields_str, *field_str;
  394. unsigned int i, j;
  395. int ret;
  396. ret = create_hitcount_val(hist_data);
  397. if (ret)
  398. goto out;
  399. fields_str = hist_data->attrs->vals_str;
  400. if (!fields_str)
  401. goto out;
  402. strsep(&fields_str, "=");
  403. if (!fields_str)
  404. goto out;
  405. for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
  406. j < TRACING_MAP_VALS_MAX; i++) {
  407. field_str = strsep(&fields_str, ",");
  408. if (!field_str)
  409. break;
  410. if (strcmp(field_str, "hitcount") == 0)
  411. continue;
  412. ret = create_val_field(hist_data, j++, file, field_str);
  413. if (ret)
  414. goto out;
  415. }
  416. if (fields_str && (strcmp(fields_str, "hitcount") != 0))
  417. ret = -EINVAL;
  418. out:
  419. return ret;
  420. }
  421. static int create_key_field(struct hist_trigger_data *hist_data,
  422. unsigned int key_idx,
  423. unsigned int key_offset,
  424. struct trace_event_file *file,
  425. char *field_str)
  426. {
  427. struct ftrace_event_field *field = NULL;
  428. unsigned long flags = 0;
  429. unsigned int key_size;
  430. int ret = 0;
  431. if (WARN_ON(key_idx >= TRACING_MAP_FIELDS_MAX))
  432. return -EINVAL;
  433. flags |= HIST_FIELD_FL_KEY;
  434. if (strcmp(field_str, "stacktrace") == 0) {
  435. flags |= HIST_FIELD_FL_STACKTRACE;
  436. key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
  437. } else {
  438. char *field_name = strsep(&field_str, ".");
  439. if (field_str) {
  440. if (strcmp(field_str, "hex") == 0)
  441. flags |= HIST_FIELD_FL_HEX;
  442. else if (strcmp(field_str, "sym") == 0)
  443. flags |= HIST_FIELD_FL_SYM;
  444. else if (strcmp(field_str, "sym-offset") == 0)
  445. flags |= HIST_FIELD_FL_SYM_OFFSET;
  446. else if ((strcmp(field_str, "execname") == 0) &&
  447. (strcmp(field_name, "common_pid") == 0))
  448. flags |= HIST_FIELD_FL_EXECNAME;
  449. else if (strcmp(field_str, "syscall") == 0)
  450. flags |= HIST_FIELD_FL_SYSCALL;
  451. else if (strcmp(field_str, "log2") == 0)
  452. flags |= HIST_FIELD_FL_LOG2;
  453. else {
  454. ret = -EINVAL;
  455. goto out;
  456. }
  457. }
  458. field = trace_find_event_field(file->event_call, field_name);
  459. if (!field || !field->size) {
  460. ret = -EINVAL;
  461. goto out;
  462. }
  463. if (is_string_field(field))
  464. key_size = MAX_FILTER_STR_VAL;
  465. else
  466. key_size = field->size;
  467. }
  468. hist_data->fields[key_idx] = create_hist_field(field, flags);
  469. if (!hist_data->fields[key_idx]) {
  470. ret = -ENOMEM;
  471. goto out;
  472. }
  473. key_size = ALIGN(key_size, sizeof(u64));
  474. hist_data->fields[key_idx]->size = key_size;
  475. hist_data->fields[key_idx]->offset = key_offset;
  476. hist_data->key_size += key_size;
  477. if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
  478. ret = -EINVAL;
  479. goto out;
  480. }
  481. hist_data->n_keys++;
  482. if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
  483. return -EINVAL;
  484. ret = key_size;
  485. out:
  486. return ret;
  487. }
  488. static int create_key_fields(struct hist_trigger_data *hist_data,
  489. struct trace_event_file *file)
  490. {
  491. unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
  492. char *fields_str, *field_str;
  493. int ret = -EINVAL;
  494. fields_str = hist_data->attrs->keys_str;
  495. if (!fields_str)
  496. goto out;
  497. strsep(&fields_str, "=");
  498. if (!fields_str)
  499. goto out;
  500. for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
  501. field_str = strsep(&fields_str, ",");
  502. if (!field_str)
  503. break;
  504. ret = create_key_field(hist_data, i, key_offset,
  505. file, field_str);
  506. if (ret < 0)
  507. goto out;
  508. key_offset += ret;
  509. }
  510. if (fields_str) {
  511. ret = -EINVAL;
  512. goto out;
  513. }
  514. ret = 0;
  515. out:
  516. return ret;
  517. }
  518. static int create_hist_fields(struct hist_trigger_data *hist_data,
  519. struct trace_event_file *file)
  520. {
  521. int ret;
  522. ret = create_val_fields(hist_data, file);
  523. if (ret)
  524. goto out;
  525. ret = create_key_fields(hist_data, file);
  526. if (ret)
  527. goto out;
  528. hist_data->n_fields = hist_data->n_vals + hist_data->n_keys;
  529. out:
  530. return ret;
  531. }
  532. static int is_descending(const char *str)
  533. {
  534. if (!str)
  535. return 0;
  536. if (strcmp(str, "descending") == 0)
  537. return 1;
  538. if (strcmp(str, "ascending") == 0)
  539. return 0;
  540. return -EINVAL;
  541. }
  542. static int create_sort_keys(struct hist_trigger_data *hist_data)
  543. {
  544. char *fields_str = hist_data->attrs->sort_key_str;
  545. struct ftrace_event_field *field = NULL;
  546. struct tracing_map_sort_key *sort_key;
  547. int descending, ret = 0;
  548. unsigned int i, j;
  549. hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
  550. if (!fields_str)
  551. goto out;
  552. strsep(&fields_str, "=");
  553. if (!fields_str) {
  554. ret = -EINVAL;
  555. goto out;
  556. }
  557. for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
  558. char *field_str, *field_name;
  559. sort_key = &hist_data->sort_keys[i];
  560. field_str = strsep(&fields_str, ",");
  561. if (!field_str) {
  562. if (i == 0)
  563. ret = -EINVAL;
  564. break;
  565. }
  566. if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
  567. ret = -EINVAL;
  568. break;
  569. }
  570. field_name = strsep(&field_str, ".");
  571. if (!field_name) {
  572. ret = -EINVAL;
  573. break;
  574. }
  575. if (strcmp(field_name, "hitcount") == 0) {
  576. descending = is_descending(field_str);
  577. if (descending < 0) {
  578. ret = descending;
  579. break;
  580. }
  581. sort_key->descending = descending;
  582. continue;
  583. }
  584. for (j = 1; j < hist_data->n_fields; j++) {
  585. field = hist_data->fields[j]->field;
  586. if (field && (strcmp(field_name, field->name) == 0)) {
  587. sort_key->field_idx = j;
  588. descending = is_descending(field_str);
  589. if (descending < 0) {
  590. ret = descending;
  591. goto out;
  592. }
  593. sort_key->descending = descending;
  594. break;
  595. }
  596. }
  597. if (j == hist_data->n_fields) {
  598. ret = -EINVAL;
  599. break;
  600. }
  601. }
  602. hist_data->n_sort_keys = i;
  603. out:
  604. return ret;
  605. }
  606. static void destroy_hist_data(struct hist_trigger_data *hist_data)
  607. {
  608. destroy_hist_trigger_attrs(hist_data->attrs);
  609. destroy_hist_fields(hist_data);
  610. tracing_map_destroy(hist_data->map);
  611. kfree(hist_data);
  612. }
  613. static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
  614. {
  615. struct tracing_map *map = hist_data->map;
  616. struct ftrace_event_field *field;
  617. struct hist_field *hist_field;
  618. int i, idx;
  619. for_each_hist_field(i, hist_data) {
  620. hist_field = hist_data->fields[i];
  621. if (hist_field->flags & HIST_FIELD_FL_KEY) {
  622. tracing_map_cmp_fn_t cmp_fn;
  623. field = hist_field->field;
  624. if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
  625. cmp_fn = tracing_map_cmp_none;
  626. else if (is_string_field(field))
  627. cmp_fn = tracing_map_cmp_string;
  628. else
  629. cmp_fn = tracing_map_cmp_num(field->size,
  630. field->is_signed);
  631. idx = tracing_map_add_key_field(map,
  632. hist_field->offset,
  633. cmp_fn);
  634. } else
  635. idx = tracing_map_add_sum_field(map);
  636. if (idx < 0)
  637. return idx;
  638. }
  639. return 0;
  640. }
  641. static bool need_tracing_map_ops(struct hist_trigger_data *hist_data)
  642. {
  643. struct hist_field *key_field;
  644. unsigned int i;
  645. for_each_hist_key_field(i, hist_data) {
  646. key_field = hist_data->fields[i];
  647. if (key_field->flags & HIST_FIELD_FL_EXECNAME)
  648. return true;
  649. }
  650. return false;
  651. }
  652. static struct hist_trigger_data *
  653. create_hist_data(unsigned int map_bits,
  654. struct hist_trigger_attrs *attrs,
  655. struct trace_event_file *file)
  656. {
  657. const struct tracing_map_ops *map_ops = NULL;
  658. struct hist_trigger_data *hist_data;
  659. int ret = 0;
  660. hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
  661. if (!hist_data)
  662. return ERR_PTR(-ENOMEM);
  663. hist_data->attrs = attrs;
  664. ret = create_hist_fields(hist_data, file);
  665. if (ret)
  666. goto free;
  667. ret = create_sort_keys(hist_data);
  668. if (ret)
  669. goto free;
  670. if (need_tracing_map_ops(hist_data))
  671. map_ops = &hist_trigger_elt_comm_ops;
  672. hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
  673. map_ops, hist_data);
  674. if (IS_ERR(hist_data->map)) {
  675. ret = PTR_ERR(hist_data->map);
  676. hist_data->map = NULL;
  677. goto free;
  678. }
  679. ret = create_tracing_map_fields(hist_data);
  680. if (ret)
  681. goto free;
  682. ret = tracing_map_init(hist_data->map);
  683. if (ret)
  684. goto free;
  685. hist_data->event_file = file;
  686. out:
  687. return hist_data;
  688. free:
  689. hist_data->attrs = NULL;
  690. destroy_hist_data(hist_data);
  691. hist_data = ERR_PTR(ret);
  692. goto out;
  693. }
  694. static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
  695. struct tracing_map_elt *elt,
  696. void *rec)
  697. {
  698. struct hist_field *hist_field;
  699. unsigned int i;
  700. u64 hist_val;
  701. for_each_hist_val_field(i, hist_data) {
  702. hist_field = hist_data->fields[i];
  703. hist_val = hist_field->fn(hist_field, rec);
  704. tracing_map_update_sum(elt, i, hist_val);
  705. }
  706. }
  707. static inline void add_to_key(char *compound_key, void *key,
  708. struct hist_field *key_field, void *rec)
  709. {
  710. size_t size = key_field->size;
  711. if (key_field->flags & HIST_FIELD_FL_STRING) {
  712. struct ftrace_event_field *field;
  713. field = key_field->field;
  714. if (field->filter_type == FILTER_DYN_STRING)
  715. size = *(u32 *)(rec + field->offset) >> 16;
  716. else if (field->filter_type == FILTER_PTR_STRING)
  717. size = strlen(key);
  718. else if (field->filter_type == FILTER_STATIC_STRING)
  719. size = field->size;
  720. /* ensure NULL-termination */
  721. if (size > key_field->size - 1)
  722. size = key_field->size - 1;
  723. }
  724. memcpy(compound_key + key_field->offset, key, size);
  725. }
  726. static void event_hist_trigger(struct event_trigger_data *data, void *rec)
  727. {
  728. struct hist_trigger_data *hist_data = data->private_data;
  729. bool use_compound_key = (hist_data->n_keys > 1);
  730. unsigned long entries[HIST_STACKTRACE_DEPTH];
  731. char compound_key[HIST_KEY_SIZE_MAX];
  732. struct stack_trace stacktrace;
  733. struct hist_field *key_field;
  734. struct tracing_map_elt *elt;
  735. u64 field_contents;
  736. void *key = NULL;
  737. unsigned int i;
  738. memset(compound_key, 0, hist_data->key_size);
  739. for_each_hist_key_field(i, hist_data) {
  740. key_field = hist_data->fields[i];
  741. if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
  742. stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
  743. stacktrace.entries = entries;
  744. stacktrace.nr_entries = 0;
  745. stacktrace.skip = HIST_STACKTRACE_SKIP;
  746. memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
  747. save_stack_trace(&stacktrace);
  748. key = entries;
  749. } else {
  750. field_contents = key_field->fn(key_field, rec);
  751. if (key_field->flags & HIST_FIELD_FL_STRING) {
  752. key = (void *)(unsigned long)field_contents;
  753. use_compound_key = true;
  754. } else
  755. key = (void *)&field_contents;
  756. }
  757. if (use_compound_key)
  758. add_to_key(compound_key, key, key_field, rec);
  759. }
  760. if (use_compound_key)
  761. key = compound_key;
  762. elt = tracing_map_insert(hist_data->map, key);
  763. if (elt)
  764. hist_trigger_elt_update(hist_data, elt, rec);
  765. }
  766. static void hist_trigger_stacktrace_print(struct seq_file *m,
  767. unsigned long *stacktrace_entries,
  768. unsigned int max_entries)
  769. {
  770. char str[KSYM_SYMBOL_LEN];
  771. unsigned int spaces = 8;
  772. unsigned int i;
  773. for (i = 0; i < max_entries; i++) {
  774. if (stacktrace_entries[i] == ULONG_MAX)
  775. return;
  776. seq_printf(m, "%*c", 1 + spaces, ' ');
  777. sprint_symbol(str, stacktrace_entries[i]);
  778. seq_printf(m, "%s\n", str);
  779. }
  780. }
  781. static void
  782. hist_trigger_entry_print(struct seq_file *m,
  783. struct hist_trigger_data *hist_data, void *key,
  784. struct tracing_map_elt *elt)
  785. {
  786. struct hist_field *key_field;
  787. char str[KSYM_SYMBOL_LEN];
  788. bool multiline = false;
  789. unsigned int i;
  790. u64 uval;
  791. seq_puts(m, "{ ");
  792. for_each_hist_key_field(i, hist_data) {
  793. key_field = hist_data->fields[i];
  794. if (i > hist_data->n_vals)
  795. seq_puts(m, ", ");
  796. if (key_field->flags & HIST_FIELD_FL_HEX) {
  797. uval = *(u64 *)(key + key_field->offset);
  798. seq_printf(m, "%s: %llx",
  799. key_field->field->name, uval);
  800. } else if (key_field->flags & HIST_FIELD_FL_SYM) {
  801. uval = *(u64 *)(key + key_field->offset);
  802. sprint_symbol_no_offset(str, uval);
  803. seq_printf(m, "%s: [%llx] %-45s",
  804. key_field->field->name, uval, str);
  805. } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
  806. uval = *(u64 *)(key + key_field->offset);
  807. sprint_symbol(str, uval);
  808. seq_printf(m, "%s: [%llx] %-55s",
  809. key_field->field->name, uval, str);
  810. } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
  811. char *comm = elt->private_data;
  812. uval = *(u64 *)(key + key_field->offset);
  813. seq_printf(m, "%s: %-16s[%10llu]",
  814. key_field->field->name, comm, uval);
  815. } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
  816. const char *syscall_name;
  817. uval = *(u64 *)(key + key_field->offset);
  818. syscall_name = get_syscall_name(uval);
  819. if (!syscall_name)
  820. syscall_name = "unknown_syscall";
  821. seq_printf(m, "%s: %-30s[%3llu]",
  822. key_field->field->name, syscall_name, uval);
  823. } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
  824. seq_puts(m, "stacktrace:\n");
  825. hist_trigger_stacktrace_print(m,
  826. key + key_field->offset,
  827. HIST_STACKTRACE_DEPTH);
  828. multiline = true;
  829. } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
  830. seq_printf(m, "%s: ~ 2^%-2llu", key_field->field->name,
  831. *(u64 *)(key + key_field->offset));
  832. } else if (key_field->flags & HIST_FIELD_FL_STRING) {
  833. seq_printf(m, "%s: %-50s", key_field->field->name,
  834. (char *)(key + key_field->offset));
  835. } else {
  836. uval = *(u64 *)(key + key_field->offset);
  837. seq_printf(m, "%s: %10llu", key_field->field->name,
  838. uval);
  839. }
  840. }
  841. if (!multiline)
  842. seq_puts(m, " ");
  843. seq_puts(m, "}");
  844. seq_printf(m, " hitcount: %10llu",
  845. tracing_map_read_sum(elt, HITCOUNT_IDX));
  846. for (i = 1; i < hist_data->n_vals; i++) {
  847. if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
  848. seq_printf(m, " %s: %10llx",
  849. hist_data->fields[i]->field->name,
  850. tracing_map_read_sum(elt, i));
  851. } else {
  852. seq_printf(m, " %s: %10llu",
  853. hist_data->fields[i]->field->name,
  854. tracing_map_read_sum(elt, i));
  855. }
  856. }
  857. seq_puts(m, "\n");
  858. }
  859. static int print_entries(struct seq_file *m,
  860. struct hist_trigger_data *hist_data)
  861. {
  862. struct tracing_map_sort_entry **sort_entries = NULL;
  863. struct tracing_map *map = hist_data->map;
  864. int i, n_entries;
  865. n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
  866. hist_data->n_sort_keys,
  867. &sort_entries);
  868. if (n_entries < 0)
  869. return n_entries;
  870. for (i = 0; i < n_entries; i++)
  871. hist_trigger_entry_print(m, hist_data,
  872. sort_entries[i]->key,
  873. sort_entries[i]->elt);
  874. tracing_map_destroy_sort_entries(sort_entries, n_entries);
  875. return n_entries;
  876. }
  877. static void hist_trigger_show(struct seq_file *m,
  878. struct event_trigger_data *data, int n)
  879. {
  880. struct hist_trigger_data *hist_data;
  881. int n_entries, ret = 0;
  882. if (n > 0)
  883. seq_puts(m, "\n\n");
  884. seq_puts(m, "# event histogram\n#\n# trigger info: ");
  885. data->ops->print(m, data->ops, data);
  886. seq_puts(m, "#\n\n");
  887. hist_data = data->private_data;
  888. n_entries = print_entries(m, hist_data);
  889. if (n_entries < 0) {
  890. ret = n_entries;
  891. n_entries = 0;
  892. }
  893. seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
  894. (u64)atomic64_read(&hist_data->map->hits),
  895. n_entries, (u64)atomic64_read(&hist_data->map->drops));
  896. }
  897. static int hist_show(struct seq_file *m, void *v)
  898. {
  899. struct event_trigger_data *data;
  900. struct trace_event_file *event_file;
  901. int n = 0, ret = 0;
  902. mutex_lock(&event_mutex);
  903. event_file = event_file_data(m->private);
  904. if (unlikely(!event_file)) {
  905. ret = -ENODEV;
  906. goto out_unlock;
  907. }
  908. list_for_each_entry_rcu(data, &event_file->triggers, list) {
  909. if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
  910. hist_trigger_show(m, data, n++);
  911. }
  912. out_unlock:
  913. mutex_unlock(&event_mutex);
  914. return ret;
  915. }
  916. static int event_hist_open(struct inode *inode, struct file *file)
  917. {
  918. return single_open(file, hist_show, file);
  919. }
  920. const struct file_operations event_hist_fops = {
  921. .open = event_hist_open,
  922. .read = seq_read,
  923. .llseek = seq_lseek,
  924. .release = single_release,
  925. };
  926. static const char *get_hist_field_flags(struct hist_field *hist_field)
  927. {
  928. const char *flags_str = NULL;
  929. if (hist_field->flags & HIST_FIELD_FL_HEX)
  930. flags_str = "hex";
  931. else if (hist_field->flags & HIST_FIELD_FL_SYM)
  932. flags_str = "sym";
  933. else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
  934. flags_str = "sym-offset";
  935. else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
  936. flags_str = "execname";
  937. else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
  938. flags_str = "syscall";
  939. else if (hist_field->flags & HIST_FIELD_FL_LOG2)
  940. flags_str = "log2";
  941. return flags_str;
  942. }
  943. static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
  944. {
  945. seq_printf(m, "%s", hist_field->field->name);
  946. if (hist_field->flags) {
  947. const char *flags_str = get_hist_field_flags(hist_field);
  948. if (flags_str)
  949. seq_printf(m, ".%s", flags_str);
  950. }
  951. }
  952. static int event_hist_trigger_print(struct seq_file *m,
  953. struct event_trigger_ops *ops,
  954. struct event_trigger_data *data)
  955. {
  956. struct hist_trigger_data *hist_data = data->private_data;
  957. struct hist_field *key_field;
  958. unsigned int i;
  959. seq_puts(m, "hist:");
  960. if (data->name)
  961. seq_printf(m, "%s:", data->name);
  962. seq_puts(m, "keys=");
  963. for_each_hist_key_field(i, hist_data) {
  964. key_field = hist_data->fields[i];
  965. if (i > hist_data->n_vals)
  966. seq_puts(m, ",");
  967. if (key_field->flags & HIST_FIELD_FL_STACKTRACE)
  968. seq_puts(m, "stacktrace");
  969. else
  970. hist_field_print(m, key_field);
  971. }
  972. seq_puts(m, ":vals=");
  973. for_each_hist_val_field(i, hist_data) {
  974. if (i == HITCOUNT_IDX)
  975. seq_puts(m, "hitcount");
  976. else {
  977. seq_puts(m, ",");
  978. hist_field_print(m, hist_data->fields[i]);
  979. }
  980. }
  981. seq_puts(m, ":sort=");
  982. for (i = 0; i < hist_data->n_sort_keys; i++) {
  983. struct tracing_map_sort_key *sort_key;
  984. sort_key = &hist_data->sort_keys[i];
  985. if (i > 0)
  986. seq_puts(m, ",");
  987. if (sort_key->field_idx == HITCOUNT_IDX)
  988. seq_puts(m, "hitcount");
  989. else {
  990. unsigned int idx = sort_key->field_idx;
  991. if (WARN_ON(idx >= TRACING_MAP_FIELDS_MAX))
  992. return -EINVAL;
  993. hist_field_print(m, hist_data->fields[idx]);
  994. }
  995. if (sort_key->descending)
  996. seq_puts(m, ".descending");
  997. }
  998. seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
  999. if (data->filter_str)
  1000. seq_printf(m, " if %s", data->filter_str);
  1001. if (data->paused)
  1002. seq_puts(m, " [paused]");
  1003. else
  1004. seq_puts(m, " [active]");
  1005. seq_putc(m, '\n');
  1006. return 0;
  1007. }
  1008. static int event_hist_trigger_init(struct event_trigger_ops *ops,
  1009. struct event_trigger_data *data)
  1010. {
  1011. struct hist_trigger_data *hist_data = data->private_data;
  1012. if (!data->ref && hist_data->attrs->name)
  1013. save_named_trigger(hist_data->attrs->name, data);
  1014. data->ref++;
  1015. return 0;
  1016. }
  1017. static void event_hist_trigger_free(struct event_trigger_ops *ops,
  1018. struct event_trigger_data *data)
  1019. {
  1020. struct hist_trigger_data *hist_data = data->private_data;
  1021. if (WARN_ON_ONCE(data->ref <= 0))
  1022. return;
  1023. data->ref--;
  1024. if (!data->ref) {
  1025. if (data->name)
  1026. del_named_trigger(data);
  1027. trigger_data_free(data);
  1028. destroy_hist_data(hist_data);
  1029. }
  1030. }
  1031. static struct event_trigger_ops event_hist_trigger_ops = {
  1032. .func = event_hist_trigger,
  1033. .print = event_hist_trigger_print,
  1034. .init = event_hist_trigger_init,
  1035. .free = event_hist_trigger_free,
  1036. };
  1037. static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
  1038. struct event_trigger_data *data)
  1039. {
  1040. data->ref++;
  1041. save_named_trigger(data->named_data->name, data);
  1042. event_hist_trigger_init(ops, data->named_data);
  1043. return 0;
  1044. }
  1045. static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
  1046. struct event_trigger_data *data)
  1047. {
  1048. if (WARN_ON_ONCE(data->ref <= 0))
  1049. return;
  1050. event_hist_trigger_free(ops, data->named_data);
  1051. data->ref--;
  1052. if (!data->ref) {
  1053. del_named_trigger(data);
  1054. trigger_data_free(data);
  1055. }
  1056. }
  1057. static struct event_trigger_ops event_hist_trigger_named_ops = {
  1058. .func = event_hist_trigger,
  1059. .print = event_hist_trigger_print,
  1060. .init = event_hist_trigger_named_init,
  1061. .free = event_hist_trigger_named_free,
  1062. };
  1063. static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
  1064. char *param)
  1065. {
  1066. return &event_hist_trigger_ops;
  1067. }
  1068. static void hist_clear(struct event_trigger_data *data)
  1069. {
  1070. struct hist_trigger_data *hist_data = data->private_data;
  1071. if (data->name)
  1072. pause_named_trigger(data);
  1073. synchronize_sched();
  1074. tracing_map_clear(hist_data->map);
  1075. if (data->name)
  1076. unpause_named_trigger(data);
  1077. }
  1078. static bool compatible_field(struct ftrace_event_field *field,
  1079. struct ftrace_event_field *test_field)
  1080. {
  1081. if (field == test_field)
  1082. return true;
  1083. if (field == NULL || test_field == NULL)
  1084. return false;
  1085. if (strcmp(field->name, test_field->name) != 0)
  1086. return false;
  1087. if (strcmp(field->type, test_field->type) != 0)
  1088. return false;
  1089. if (field->size != test_field->size)
  1090. return false;
  1091. if (field->is_signed != test_field->is_signed)
  1092. return false;
  1093. return true;
  1094. }
  1095. static bool hist_trigger_match(struct event_trigger_data *data,
  1096. struct event_trigger_data *data_test,
  1097. struct event_trigger_data *named_data,
  1098. bool ignore_filter)
  1099. {
  1100. struct tracing_map_sort_key *sort_key, *sort_key_test;
  1101. struct hist_trigger_data *hist_data, *hist_data_test;
  1102. struct hist_field *key_field, *key_field_test;
  1103. unsigned int i;
  1104. if (named_data && (named_data != data_test) &&
  1105. (named_data != data_test->named_data))
  1106. return false;
  1107. if (!named_data && is_named_trigger(data_test))
  1108. return false;
  1109. hist_data = data->private_data;
  1110. hist_data_test = data_test->private_data;
  1111. if (hist_data->n_vals != hist_data_test->n_vals ||
  1112. hist_data->n_fields != hist_data_test->n_fields ||
  1113. hist_data->n_sort_keys != hist_data_test->n_sort_keys)
  1114. return false;
  1115. if (!ignore_filter) {
  1116. if ((data->filter_str && !data_test->filter_str) ||
  1117. (!data->filter_str && data_test->filter_str))
  1118. return false;
  1119. }
  1120. for_each_hist_field(i, hist_data) {
  1121. key_field = hist_data->fields[i];
  1122. key_field_test = hist_data_test->fields[i];
  1123. if (key_field->flags != key_field_test->flags)
  1124. return false;
  1125. if (!compatible_field(key_field->field, key_field_test->field))
  1126. return false;
  1127. if (key_field->offset != key_field_test->offset)
  1128. return false;
  1129. }
  1130. for (i = 0; i < hist_data->n_sort_keys; i++) {
  1131. sort_key = &hist_data->sort_keys[i];
  1132. sort_key_test = &hist_data_test->sort_keys[i];
  1133. if (sort_key->field_idx != sort_key_test->field_idx ||
  1134. sort_key->descending != sort_key_test->descending)
  1135. return false;
  1136. }
  1137. if (!ignore_filter && data->filter_str &&
  1138. (strcmp(data->filter_str, data_test->filter_str) != 0))
  1139. return false;
  1140. return true;
  1141. }
  1142. static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
  1143. struct event_trigger_data *data,
  1144. struct trace_event_file *file)
  1145. {
  1146. struct hist_trigger_data *hist_data = data->private_data;
  1147. struct event_trigger_data *test, *named_data = NULL;
  1148. int ret = 0;
  1149. if (hist_data->attrs->name) {
  1150. named_data = find_named_trigger(hist_data->attrs->name);
  1151. if (named_data) {
  1152. if (!hist_trigger_match(data, named_data, named_data,
  1153. true)) {
  1154. ret = -EINVAL;
  1155. goto out;
  1156. }
  1157. }
  1158. }
  1159. if (hist_data->attrs->name && !named_data)
  1160. goto new;
  1161. list_for_each_entry_rcu(test, &file->triggers, list) {
  1162. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  1163. if (!hist_trigger_match(data, test, named_data, false))
  1164. continue;
  1165. if (hist_data->attrs->pause)
  1166. test->paused = true;
  1167. else if (hist_data->attrs->cont)
  1168. test->paused = false;
  1169. else if (hist_data->attrs->clear)
  1170. hist_clear(test);
  1171. else
  1172. ret = -EEXIST;
  1173. goto out;
  1174. }
  1175. }
  1176. new:
  1177. if (hist_data->attrs->cont || hist_data->attrs->clear) {
  1178. ret = -ENOENT;
  1179. goto out;
  1180. }
  1181. if (hist_data->attrs->pause)
  1182. data->paused = true;
  1183. if (named_data) {
  1184. destroy_hist_data(data->private_data);
  1185. data->private_data = named_data->private_data;
  1186. set_named_trigger_data(data, named_data);
  1187. data->ops = &event_hist_trigger_named_ops;
  1188. }
  1189. if (data->ops->init) {
  1190. ret = data->ops->init(data->ops, data);
  1191. if (ret < 0)
  1192. goto out;
  1193. }
  1194. list_add_rcu(&data->list, &file->triggers);
  1195. ret++;
  1196. update_cond_flag(file);
  1197. if (trace_event_trigger_enable_disable(file, 1) < 0) {
  1198. list_del_rcu(&data->list);
  1199. update_cond_flag(file);
  1200. ret--;
  1201. }
  1202. out:
  1203. return ret;
  1204. }
  1205. static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
  1206. struct event_trigger_data *data,
  1207. struct trace_event_file *file)
  1208. {
  1209. struct hist_trigger_data *hist_data = data->private_data;
  1210. struct event_trigger_data *test, *named_data = NULL;
  1211. bool unregistered = false;
  1212. if (hist_data->attrs->name)
  1213. named_data = find_named_trigger(hist_data->attrs->name);
  1214. list_for_each_entry_rcu(test, &file->triggers, list) {
  1215. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  1216. if (!hist_trigger_match(data, test, named_data, false))
  1217. continue;
  1218. unregistered = true;
  1219. list_del_rcu(&test->list);
  1220. trace_event_trigger_enable_disable(file, 0);
  1221. update_cond_flag(file);
  1222. break;
  1223. }
  1224. }
  1225. if (unregistered && test->ops->free)
  1226. test->ops->free(test->ops, test);
  1227. }
  1228. static void hist_unreg_all(struct trace_event_file *file)
  1229. {
  1230. struct event_trigger_data *test, *n;
  1231. list_for_each_entry_safe(test, n, &file->triggers, list) {
  1232. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  1233. list_del_rcu(&test->list);
  1234. trace_event_trigger_enable_disable(file, 0);
  1235. update_cond_flag(file);
  1236. if (test->ops->free)
  1237. test->ops->free(test->ops, test);
  1238. }
  1239. }
  1240. }
  1241. static int event_hist_trigger_func(struct event_command *cmd_ops,
  1242. struct trace_event_file *file,
  1243. char *glob, char *cmd, char *param)
  1244. {
  1245. unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
  1246. struct event_trigger_data *trigger_data;
  1247. struct hist_trigger_attrs *attrs;
  1248. struct event_trigger_ops *trigger_ops;
  1249. struct hist_trigger_data *hist_data;
  1250. char *trigger;
  1251. int ret = 0;
  1252. if (!param)
  1253. return -EINVAL;
  1254. /* separate the trigger from the filter (k:v [if filter]) */
  1255. trigger = strsep(&param, " \t");
  1256. if (!trigger)
  1257. return -EINVAL;
  1258. attrs = parse_hist_trigger_attrs(trigger);
  1259. if (IS_ERR(attrs))
  1260. return PTR_ERR(attrs);
  1261. if (attrs->map_bits)
  1262. hist_trigger_bits = attrs->map_bits;
  1263. hist_data = create_hist_data(hist_trigger_bits, attrs, file);
  1264. if (IS_ERR(hist_data)) {
  1265. destroy_hist_trigger_attrs(attrs);
  1266. return PTR_ERR(hist_data);
  1267. }
  1268. trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
  1269. ret = -ENOMEM;
  1270. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  1271. if (!trigger_data)
  1272. goto out_free;
  1273. trigger_data->count = -1;
  1274. trigger_data->ops = trigger_ops;
  1275. trigger_data->cmd_ops = cmd_ops;
  1276. INIT_LIST_HEAD(&trigger_data->list);
  1277. RCU_INIT_POINTER(trigger_data->filter, NULL);
  1278. trigger_data->private_data = hist_data;
  1279. /* if param is non-empty, it's supposed to be a filter */
  1280. if (param && cmd_ops->set_filter) {
  1281. ret = cmd_ops->set_filter(param, trigger_data, file);
  1282. if (ret < 0)
  1283. goto out_free;
  1284. }
  1285. if (glob[0] == '!') {
  1286. cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
  1287. ret = 0;
  1288. goto out_free;
  1289. }
  1290. ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
  1291. /*
  1292. * The above returns on success the # of triggers registered,
  1293. * but if it didn't register any it returns zero. Consider no
  1294. * triggers registered a failure too.
  1295. */
  1296. if (!ret) {
  1297. if (!(attrs->pause || attrs->cont || attrs->clear))
  1298. ret = -ENOENT;
  1299. goto out_free;
  1300. } else if (ret < 0)
  1301. goto out_free;
  1302. /* Just return zero, not the number of registered triggers */
  1303. ret = 0;
  1304. out:
  1305. return ret;
  1306. out_free:
  1307. if (cmd_ops->set_filter)
  1308. cmd_ops->set_filter(NULL, trigger_data, NULL);
  1309. kfree(trigger_data);
  1310. destroy_hist_data(hist_data);
  1311. goto out;
  1312. }
  1313. static struct event_command trigger_hist_cmd = {
  1314. .name = "hist",
  1315. .trigger_type = ETT_EVENT_HIST,
  1316. .flags = EVENT_CMD_FL_NEEDS_REC,
  1317. .func = event_hist_trigger_func,
  1318. .reg = hist_register_trigger,
  1319. .unreg = hist_unregister_trigger,
  1320. .unreg_all = hist_unreg_all,
  1321. .get_trigger_ops = event_hist_get_trigger_ops,
  1322. .set_filter = set_trigger_filter,
  1323. };
  1324. __init int register_trigger_hist_cmd(void)
  1325. {
  1326. int ret;
  1327. ret = register_event_command(&trigger_hist_cmd);
  1328. WARN_ON(ret < 0);
  1329. return ret;
  1330. }
  1331. static void
  1332. hist_enable_trigger(struct event_trigger_data *data, void *rec)
  1333. {
  1334. struct enable_trigger_data *enable_data = data->private_data;
  1335. struct event_trigger_data *test;
  1336. list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
  1337. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  1338. if (enable_data->enable)
  1339. test->paused = false;
  1340. else
  1341. test->paused = true;
  1342. }
  1343. }
  1344. }
  1345. static void
  1346. hist_enable_count_trigger(struct event_trigger_data *data, void *rec)
  1347. {
  1348. if (!data->count)
  1349. return;
  1350. if (data->count != -1)
  1351. (data->count)--;
  1352. hist_enable_trigger(data, rec);
  1353. }
  1354. static struct event_trigger_ops hist_enable_trigger_ops = {
  1355. .func = hist_enable_trigger,
  1356. .print = event_enable_trigger_print,
  1357. .init = event_trigger_init,
  1358. .free = event_enable_trigger_free,
  1359. };
  1360. static struct event_trigger_ops hist_enable_count_trigger_ops = {
  1361. .func = hist_enable_count_trigger,
  1362. .print = event_enable_trigger_print,
  1363. .init = event_trigger_init,
  1364. .free = event_enable_trigger_free,
  1365. };
  1366. static struct event_trigger_ops hist_disable_trigger_ops = {
  1367. .func = hist_enable_trigger,
  1368. .print = event_enable_trigger_print,
  1369. .init = event_trigger_init,
  1370. .free = event_enable_trigger_free,
  1371. };
  1372. static struct event_trigger_ops hist_disable_count_trigger_ops = {
  1373. .func = hist_enable_count_trigger,
  1374. .print = event_enable_trigger_print,
  1375. .init = event_trigger_init,
  1376. .free = event_enable_trigger_free,
  1377. };
  1378. static struct event_trigger_ops *
  1379. hist_enable_get_trigger_ops(char *cmd, char *param)
  1380. {
  1381. struct event_trigger_ops *ops;
  1382. bool enable;
  1383. enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
  1384. if (enable)
  1385. ops = param ? &hist_enable_count_trigger_ops :
  1386. &hist_enable_trigger_ops;
  1387. else
  1388. ops = param ? &hist_disable_count_trigger_ops :
  1389. &hist_disable_trigger_ops;
  1390. return ops;
  1391. }
  1392. static void hist_enable_unreg_all(struct trace_event_file *file)
  1393. {
  1394. struct event_trigger_data *test, *n;
  1395. list_for_each_entry_safe(test, n, &file->triggers, list) {
  1396. if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
  1397. list_del_rcu(&test->list);
  1398. update_cond_flag(file);
  1399. trace_event_trigger_enable_disable(file, 0);
  1400. if (test->ops->free)
  1401. test->ops->free(test->ops, test);
  1402. }
  1403. }
  1404. }
  1405. static struct event_command trigger_hist_enable_cmd = {
  1406. .name = ENABLE_HIST_STR,
  1407. .trigger_type = ETT_HIST_ENABLE,
  1408. .func = event_enable_trigger_func,
  1409. .reg = event_enable_register_trigger,
  1410. .unreg = event_enable_unregister_trigger,
  1411. .unreg_all = hist_enable_unreg_all,
  1412. .get_trigger_ops = hist_enable_get_trigger_ops,
  1413. .set_filter = set_trigger_filter,
  1414. };
  1415. static struct event_command trigger_hist_disable_cmd = {
  1416. .name = DISABLE_HIST_STR,
  1417. .trigger_type = ETT_HIST_ENABLE,
  1418. .func = event_enable_trigger_func,
  1419. .reg = event_enable_register_trigger,
  1420. .unreg = event_enable_unregister_trigger,
  1421. .unreg_all = hist_enable_unreg_all,
  1422. .get_trigger_ops = hist_enable_get_trigger_ops,
  1423. .set_filter = set_trigger_filter,
  1424. };
  1425. static __init void unregister_trigger_hist_enable_disable_cmds(void)
  1426. {
  1427. unregister_event_command(&trigger_hist_enable_cmd);
  1428. unregister_event_command(&trigger_hist_disable_cmd);
  1429. }
  1430. __init int register_trigger_hist_enable_disable_cmds(void)
  1431. {
  1432. int ret;
  1433. ret = register_event_command(&trigger_hist_enable_cmd);
  1434. if (WARN_ON(ret < 0))
  1435. return ret;
  1436. ret = register_event_command(&trigger_hist_disable_cmd);
  1437. if (WARN_ON(ret < 0))
  1438. unregister_trigger_hist_enable_disable_cmds();
  1439. return ret;
  1440. }