From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Ahern Subject: Re: [PATCH v7 3/3] KVM: perf: kvm events analysis tool Date: Mon, 03 Sep 2012 10:04:37 -0600 Message-ID: <5044D515.9050903@gmail.com> References: <1346061106-5364-1-git-send-email-haodong@linux.vnet.ibm.com> <1346061106-5364-4-git-send-email-haodong@linux.vnet.ibm.com> <503FB105.9000205@gmail.com> <50446EEA.5060306@linux.vnet.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: avi@redhat.com, acme@infradead.org, mtosatti@redhat.com, mingo@elte.hu, xiaoguangrong@linux.vnet.ibm.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org To: don Return-path: In-Reply-To: <50446EEA.5060306@linux.vnet.ibm.com> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org On 9/3/12 2:48 AM, don wrote: > =E4=BA=8E 2012=E5=B9=B408=E6=9C=8831=E6=97=A5 02:29, David Ahern =E5=86= =99=E9=81=93: >> In addition to Andrew's comment about making the stats struct and >> functions generic... > Yes. :-) >> >> On 8/27/12 3:51 AM, Dong Hao wrote: >> ---8<--- >> >>> +static void exit_event_decode_key(struct event_key *key, char >>> decode[20]) >>> +{ >>> + const char *exit_reason =3D get_exit_reason(key->key); >>> + >>> + snprintf(decode, 20, "%s", exit_reason); >>> +} >> >> Use scnprintf rather than snprintf. > Why? Since we don't care about the return value, what's the differenc= e? 1. consistency (scnprintf is preferred). 2. what if a new exit reason is added in the future that is larger than= =20 19 characters? Better to be safe now. >> >> ---8<--- >> >>> +static bool kvm_event_expand(struct kvm_event *event, int vcpu_id) >>> +{ >>> + int old_max_vcpu =3D event->max_vcpu; >>> + >>> + if (vcpu_id < event->max_vcpu) >>> + return true; >>> + >>> + while (event->max_vcpu <=3D vcpu_id) >>> + event->max_vcpu +=3D DEFAULT_VCPU_NUM; >>> + >>> + event->vcpu =3D realloc(event->vcpu, >>> + event->max_vcpu * sizeof(*event->vcpu)); >>> + if (!event->vcpu) { >> >> If realloc fails you leak memory by overwriting the current pointer. > Thanks for pointing it out, we will terminate the running instance in > our next > version. Ok. Make sure to free the memory of the previous pointer on failure=20 before cleaning up. The idea is that all allocations are properly freed= =20 on exit. >> >> ---8<--- >> >>> +static double event_stats_stddev(int vcpu_id, struct kvm_event *ev= ent) >>> +{ >>> + struct event_stats *stats =3D &event->total; >>> + double variance, variance_mean, stddev; >>> + >>> + if (vcpu_id !=3D -1) >>> + stats =3D &event->vcpu[vcpu_id]; >>> + >>> + BUG_ON(!stats->count); >>> + >>> + variance =3D stats->M2 / (stats->count - 1); >>> + variance_mean =3D variance / stats->count; >>> + stddev =3D sqrt(variance_mean); >>> + >>> + return stddev * 100 / stats->mean; >>> +} >> >> perf should be consistent in the stddev it shows the user. Any reaso= n >> to dump relative stddev versus stddev used by perf-stat? > Since 'perf stat' uses relative standard deviation rather than stddev= , > 'perf kvm stat' > just follows the style of 'perf stat'. Yes, I've been working on an idea motivated by Andi Kleen. I have=20 noticed that perf stat also uses relative stddev just in a non-direct=20 way. I suggest moving common stats code from perf-stat to=20 utils/stat.[ch], add a rel_stddev_stats function that returns the above= =20 and have perf-kvm use that. >> ---8<--- >> >>> + /* >>> + * Append "-a" only if "-p"/"--pid" is not specified since the= y >>> + * are mutually exclusive. >>> + */ >>> + if (!kvm_record_specified_guest(argc, argv)) >>> + rec_argv[i++] =3D STRDUP_FAIL_EXIT("-a"); >> >> Other perf-kvm commands rely on perf-record semantics -- i.e., for >> user to add the -a or -p option. > You mean, remove '-a' from the default options, then: > if a user wants to record all guest he will use 'perf stat record -a'= ; > and if a user wants to record the specified guest, he should use > 'perf stat record -p xxx'? Yes. Well, 'perf kvm stat record' with a -a or -p. That makes the new=20 stat subcommand consistent with just 'perf kvm record'. > > Well, as the style of other subcommand, e.g., perf lock/perf sched, t= he > 'perf xxx record' record all events on all cpus, no need to use '-a'. > > Based on mentioned above, I prefer the original way. ;) Yes, I noticed that some commands add -a before calling cmd_record().=20 Can't change that but we can keep perf-kvm consistent with its own=20 variants which means not automagically adding -a. David