trace-cmd: add option to group like comms for profile
PATCH v2 1/1 trace-cmd: add option to group like comms for profile
When profiling large applications that have multiple children we really want to see the profile of the app all together. Add a --by-comm option to accomplish this for profile. This merges all the events for like comms into one big chunk so we can easily see what a giant app is doing, such as HHVM. Thanks,
---V1->V2:
-renamed the option to --by-comm, added it to trace-cmd report --profile as well -fixed up the string hash
-changed it to merge all events after the fact so it's less error prone
trace-hash-local.h | 9 ++ trace-local.h | 1 + trace-profile.c | 302 ++++++++++++++++++++++++++++++++++++++++++++++++++++- trace-read.c | 5 + trace-record.c | 5 + trace-usage.c | 3 +- 6 files changed, 319 insertions(+), 6 deletions(-)
trace-hash-local.h +9 -0
@@ -48,4 +48,13 @@ static inline unsigned int trace_hash(int val) return hash; } +static inline unsigned int trace_hash_str (char *str) +{ +int val =0 ; +int i; + +for (i =0 ; str[i]; i++) + val += ((int )str[i]) << (i &0xffffff ); +return trace_hash (val); +}#endif /* _TRACE_HASH_LOCAL_H */
trace-local.h +1 -0
@@ -81,6 +81,7 @@ int trace_profile_record(struct tracecmd_input *handle,void trace_init_profile (struct tracecmd_input *handle,struct hook_list *hooks,int global);int trace_profile (void ); +void trace_profile_set_merge_like_comms (void );struct tracecmd_input * trace_stream_init(struct buffer_instance *instance, int cpu, int fd, int cpus,
trace-profile.c +297 -5
391 lines not on this page · raw message
trace-read.c +5 -0
@@ -1274,6 +1274,7 @@ static void add_hook(const char *arg) }enum { + OPT_bycomm =243 , OPT_uname =244 , OPT_profile =245 , OPT_event =246 ,
@@ -1348,6 +1349,7 @@ void trace_report (int argc, char **argv) {"boundary", no_argument, NULL, OPT_boundary}, {"profile", no_argument, NULL, OPT_profile}, {"uname", no_argument, NULL, OPT_uname}, + {"by-comm", no_argument, NULL, OPT_bycomm}, {"help", no_argument, NULL, '?'}, {NULL, 0, NULL, 0} };
@@ -1477,6 +1479,9 @@ void trace_report (int argc, char **argv)case OPT_uname: show_uname =1 ;break ; +case OPT_bycomm: +trace_profile_set_merge_like_comms (); +break ;default :usage (argv); }
trace-record.c +5 -0
@@ -3703,6 +3703,7 @@ static void add_hook(struct buffer_instance *instance, const char *arg) }enum { + OPT_bycomm =250 , OPT_stderr =251 , OPT_profile =252 , OPT_nosplice =253 ,
@@ -3871,6 +3872,7 @@ void trace_record (int argc, char **argv) {"nosplice", no_argument, NULL, OPT_nosplice}, {"profile", no_argument, NULL, OPT_profile}, {"stderr", no_argument, NULL, OPT_stderr}, + {"by-comm", no_argument, NULL, OPT_bycomm}, {"help", no_argument, NULL, '?'}, {NULL, 0, NULL, 0} };
@@ -4102,6 +4104,9 @@ void trace_record (int argc, char **argv)close (1 );dup2 (2 ,1 );break ; +case OPT_bycomm: +trace_profile_set_merge_like_comms (); +break ;default :usage (argv); }
trace-usage.c +2 -1
@@ -48,6 +48,7 @@ static struct usage_help usage_help[] = {" -B create sub buffer and folling events will be enabled here\n" " -k do not reset the buffers after tracing.\n" " -i do not fail if an event is not found\n" +" --by-comm used with --profile, merge events for related comms\n" " --profile enable tracing options needed for report --profile\n" " --func-stack perform a stack trace for function tracer\n" " (use with caution)\n"
@@ -151,7 +152,7 @@ static struct usage_help usage_help[] = {" -G when profiling, set soft and hard irqs as global\n" " -H Allows users to hook two events together for timings\n" " (used with --profile)\n" - +" --by-comm used with --profile, merge events for related comms\n" }, { "stream",
Signature · 1 line
2.1.0
Review
Quoted 3 lines · On Thu, 21 May 2015 13:30:08 -0400 Josef Bacik <[email protected]> wrote:
V1->V2:(quoted diff) +0
-2-renamed the option to --by-comm, added it to trace-cmd report --profile as well -fixed up the string hash
Or break it ;-)
Quoted 10 lines
(quoted diff) +0
-1-changed it to merge all events after the fact so it's less error pronetrace-hash-local.h +0
-0@@ -48,4 +48,13 @@ static inline unsigned int trace_hash(int val)return hash; }
trace-hash-local.h static inline unsigned int trace_hash(int val) · line 60
Quoted 10 lines
trace-hash-local.h +9
-0+static inline unsigned int trace_hash_str (char *str) +{ +int val =0 ; +int i; + +for (i =0 ; str[i]; i++) + val += ((int )str[i]) << (i &0xffffff ); +return trace_hash (val); +}
I need to clean out my medicine cabinet and remove all the expired meds. Because I was definitely taking something nasty when I recommended (i & 0xffffff)!
When i is greater than 32 (which is less than 0xffffff) it will overflow the addition. What I wanted was that we don't shift more than 24 bits. Where 2 ** 24 - 1 == 0xffffff.
That should be:
val += ((int)str[i]) << (i % 25);
my bad :-/
To avoid the slow '%', I'll just use '& 0xf', as shifting it 16 times is enough for this algorithm.
No need to send a new patch, I'll fix it, as it was my brain fart. I'll review the rest of this patch too and apply it if nothing sticks out.
Thanks!
-- Steve
trace-profile.c static int compare_tasks(const void *a, const void *b) · line 1987
Quoted 6 lines · On Thu, 21 May 2015 13:30:08 -0400 Josef Bacik <[email protected]> wrote:
trace-profile.c +6
-0+static int compare_groups (const void *a,const void *b) +{ +const char *A = a; +const char *B = b; + +return strcmp (A ,B );
a and b are not strings here. They are group_data pointers.
I think what you want is this:
static int compare_groups(const void *a, const void *b)
{
struct group_data * const *A = a;
struct group_data * const *B = b;
return strcmp((*A)->comm, (*B)->comm);
}-- Steve
Quoted 44 lines
trace-profile.c +40
-0+} + +static void output_groups (struct handle_data *h) +{ +struct trace_hash_item **bucket; +struct trace_hash_item *item; +struct group_data **groups; +int nr_groups =0 ; +int i; + +trace_hash_for_each_bucket (bucket, &h->group_hash) { +trace_hash_for_each_item (item, bucket) { + nr_groups++; + } + } + +if (nr_groups ==0 ) +return ; + + groups =malloc_or_die (sizeof (*groups) * nr_groups); + + nr_groups =0 ; + +trace_hash_for_each_bucket (bucket, &h->group_hash) { + trace_hash_while_item(item, bucket) { + groups[nr_groups++] =group_from_item (item); +trace_hash_del (item); + } + } + +qsort (groups, nr_groups,sizeof (*groups), compare_groups); + +for (i =0 ; i < nr_groups; i++) { +output_group (h, groups[i]); +free_group (groups[i]); + } + +free (groups); +} +
Quoted 11 lines · On Thu, 21 May 2015 13:30:08 -0400 Josef Bacik <[email protected]> wrote:
trace-profile.c +11
-0+static void merge_tasks (struct handle_data *h) +{ +struct trace_hash_item **bucket; +struct trace_hash_item *item; + +if (!merge_like_comms) +return ; + +trace_hash_for_each_bucket (bucket, &h->task_hash) { +trace_hash_for_each_item (item, bucket) +add_group (h,task_from_item (item));
trace-profile.c static void output_handle(struct handle_data *h) · line 2323
Quoted 10 lines
trace-profile.c +5
-0+ } +} +int trace_profile (void ) {struct handle_data *h;for (h = handles; h; h = h->next) { +if (merge_like_comms) +merge_tasks (h);
I don't think we need the double check. Here you only call merge_tasks() if merge_like_comms is set, but then the first thing you do in merge_tasks() is to return if merge_like_comms is not set. One check is enough.
-- Steve
Quoted 3 lines
trace-profile.c +0
-0output_handle (h);trace_hash_free (&h->task_hash); }