perf tools: Experiment with cppcheck
tip-bot for Arnaldo Carvalho de Melo
merge botCommit-ID: 18ef15c675a5d5d97f844ebcf340a2a6c7cf3142 Gitweb: http://git.kernel.org/tip/18ef15c675a5d5d97f844ebcf340a2a6c7cf3142 Author: Arnaldo Carvalho de Melo <[email protected]> AuthorDate: Mon, 3 Oct 2016 11:07:24 -0300 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 3 Oct 2016 11:24:13 -0300
perf tools: Experiment with cppcheck
Experimenting a bit using cppcheck[1], a static checker brought to my attention by Colin, reducing the scope of some variables, reducing the line of source code lines in the process:
$ cppcheck --enable=style tools/perf/util/thread.c Checking tools/perf/util/thread.c... [tools/perf/util/thread.c:17]: (style) The scope of the variable 'leader' can be reduced. [tools/perf/util/thread.c:133]: (style) The scope of the variable 'err' can be reduced. [tools/perf/util/thread.c:273]: (style) The scope of the variable 'err' can be reduced.
Will continue later, but these are already useful, keep them.
1: https://sourceforge.net/p/cppcheck/wiki/Home/
Cc Adrian Hunter <[email protected]>
Cc Colin Ian King <[email protected]>
Cc David Ahern <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Wang Nan <[email protected]>
Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
--- tools/perf/util/evlist.c | 12 +++++------- tools/perf/util/evsel.c | 3 +-- tools/perf/util/machine.c | 6 +++--- tools/perf/util/strbuf.h | 3 +-- tools/perf/util/thread.c | 9 +++------ 5 files changed, 13 insertions(+), 20 deletions(-)
tools/perf/util/evlist.c +5 -7
@@ -384,15 +384,14 @@ void perf_evlist__toggle_enable(struct perf_evlist *evlist)static int perf_evlist__enable_event_cpu (struct perf_evlist *evlist,struct perf_evsel *evsel,int cpu) { -int thread, err; +int thread;int nr_threads =perf_evlist__nr_threads (evlist, evsel);if (!evsel->fd)return -EINVAL ;for (thread =0 ; thread < nr_threads; thread++) { - err =ioctl (FD (evsel, cpu, thread), -PERF_EVENT_IOC_ENABLE ,0 ); +int err =ioctl (FD (evsel, cpu, thread),PERF_EVENT_IOC_ENABLE ,0 );if (err)return err; }
@@ -403,14 +402,14 @@ static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,struct perf_evsel *evsel, int thread) { - int cpu, err; + int cpu;int nr_cpus =cpu_map__nr (evlist->cpus);if (!evsel->fd)return -EINVAL ;for (cpu =0 ; cpu < nr_cpus; cpu++) { - err =ioctl (FD (evsel, cpu, thread),PERF_EVENT_IOC_ENABLE ,0 ); +int err =ioctl (FD (evsel, cpu, thread),PERF_EVENT_IOC_ENABLE ,0 );if (err)return err; }
@@ -1606,10 +1605,9 @@ void perf_evlist__close(struct perf_evlist *evlist)struct perf_evsel *evsel;int ncpus =cpu_map__nr (evlist->cpus);int nthreads =thread_map__nr (evlist->threads); -int n;evlist__for_each_entry_reverse (evlist, evsel) { - n = evsel->cpus ? evsel->cpus->nr : ncpus; +int n = evsel->cpus ? evsel->cpus->nr : ncpus;perf_evsel__close (evsel, n, nthreads); } }
tools/perf/util/evsel.c +1 -2
@@ -985,14 +985,13 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,static int perf_evsel__alloc_fd (struct perf_evsel *evsel,int ncpus,int nthreads) { -int cpu, thread; -if (evsel->system_wide) nthreads =1 ; evsel->fd =xyarray__new (ncpus, nthreads,sizeof (int ));if (evsel->fd) { +int cpu, thread;for (cpu =0 ; cpu < ncpus; cpu++) {for (thread =0 ; thread < nthreads; thread++) {FD (evsel, cpu, thread) =-1 ;
tools/perf/util/machine.c +3 -3
@@ -1745,9 +1745,8 @@ static int resolve_lbr_callchain_sample(struct thread *thread, int max_stack) {struct ip_callchain *chain = sample->callchain; -int chain_nr =min (max_stack, (int )chain->nr); +int chain_nr =min (max_stack, (int )chain->nr), i;u8 cpumode =PERF_RECORD_MISC_USER ; -int i, j, err;u64 ip;for (i =0 ; i < chain_nr; i++) {
@@ -1758,7 +1757,7 @@ static int resolve_lbr_callchain_sample(struct thread *thread,/* LBR only affects the user callchain */ if (i != chain_nr) {struct branch_stack *lbr_stack = sample->branch_stack; -int lbr_nr = lbr_stack->nr; +int lbr_nr = lbr_stack->nr, j;/* * LBR callstack can only get user call chain. * The mix_chain_nr is kernel call chain
@@ -1772,6 +1771,7 @@ static int resolve_lbr_callchain_sample(struct thread *thread,int mix_chain_nr = i +1 + lbr_nr +1 ;for (j =0 ; j < mix_chain_nr; j++) { +int err;if (callchain_param.order ==ORDER_CALLEE ) {if (j < i +1 ) ip = chain->ips[j];
tools/perf/util/strbuf.h +1 -2
@@ -66,9 +66,8 @@ static inline ssize_t strbuf_avail(const struct strbuf *sb) {int strbuf_grow (struct strbuf *buf,size_t );static inline int strbuf_setlen (struct strbuf *sb,size_t len) { -int ret;if (!sb->alloc) { - ret =strbuf_grow (sb,0 ); +int ret =strbuf_grow (sb,0 );if (ret)return ret; }
tools/perf/util/thread.c +3 -6
@@ -14,13 +14,12 @@int thread__init_map_groups (struct thread *thread,struct machine *machine) { -struct thread *leader;pid_t pid = thread->pid_;if (pid == thread->tid || pid ==-1 ) { thread->mg =map_groups__new (machine); }else { - leader =__machine__findnew_thread (machine, pid, pid); +struct thread *leader =__machine__findnew_thread (machine, pid, pid);if (leader) { thread->mg =map_groups__get (leader->mg);thread__put (leader);
@@ -130,11 +129,10 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, bool exec) {struct comm *new, *curr =thread__comm (thread); -int err;/* Override the default :tid entry */ if (!thread->comm_set) { - err =comm__override (curr, str, timestamp, exec); +int err =comm__override (curr, str, timestamp, exec);if (err)return err; }else {
@@ -270,10 +268,9 @@ static int thread__clone_map_groups(struct thread *thread,int thread__fork (struct thread *thread,struct thread *parent,u64 timestamp) { -int err; -if (parent->comm_set) {const char *comm =thread__comm_str (parent); +int err;if (!comm)return -ENOMEM ; err =thread__set_comm (thread, comm, timestamp);