perf: Pass 0 as a third argument to ioctl on perf_fd
Namhyung Kim
PATCH 1/2 perf: Pass 0 as a third argument to ioctl on perf_fd
The third argument of PERF_EVENT_IOC_{ENABLE,DISABLE} ioctl is a flag to determine whether the command should be applied to group members. Since the powertop doesn't use the event grouping feature, it'd better passing 0 as a third argument explicitly.
--- src/perf/perf.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/perf/perf.cpp +2 -2
@@ -128,7 +128,7 @@ void perf_event::create_perf_event(char *eventname, int _cpu) return; } - ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE); + ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0); if (ret < 0) { fprintf(stderr, "failed to enable perf \n");
@@ -210,7 +210,7 @@ void perf_event::start(void) void perf_event::stop(void) { int ret; - ret = ioctl(perf_fd, PERF_EVENT_IOC_DISABLE); + ret = ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0); if (ret) cout << "stop failing\n"; }
Signature · 1 line
1.7.9.2
Namhyung Kim
PATCH 2/2 perf: Check return value of strdup
The strdup() can return NULL so that the result should be checked.
--- src/perf/perf.cpp | 5 +++++ 1 file changed, 5 insertions(+)
src/perf/perf.cpp +5 -0
@@ -145,6 +145,11 @@ void perf_event::set_event_name(const char *event_name) if (name) free(name); name = strdup(event_name); + if (!name) { + fprintf(stderr, "failed to allocate event name\n"); + return; + } + char *c; c = strchr(name, ':');
Signature · 1 line
1.7.9.2
Sergey Senozhatsky
Re: [Powertop] [PATCH 2/2] perf: Check return value of strdup
Quoted 4 lines · Namhyung Kim
The
strdup()can return NULL so that the result should be checked.Signed-off-by Namhyung Kim <namhyung(a)gmail.com>---
looks good to me, thank you.
-ss
Quoted 19 lines
src/perf/perf.cpp | 5 +++++ 1 file changed, 5 insertions(+)src/perf/perf.cpp +5
-0@@ -145,6 +145,11 @@ void perf_event::set_event_name(const char *event_name) if (name) free(name); name = strdup(event_name); + if (!name) { + fprintf(stderr, "failed to allocate event name\n"); + return; + } + char *c; c = strchr(name, ':');
Chris Ferron
Re: [Powertop] [PATCH 2/2] perf: Check return value of strdup
In reply to Namhyung KimQuoted 13 lines · Namhyung Kim
The
strdup()can return NULL so that the result should be checked.Signed-off-by Namhyung Kim <namhyung(a)gmail.com>--- src/perf/perf.cpp | 5 +++++ 1 file changed, 5 insertions(+)src/perf/perf.cpp +0
-0@@ -145,6 +145,11 @@ void perf_event::set_event_name(const char *event_name) if (name)
src/perf/perf.cpp void perf_event::set_event_name(const char *event_name) · line 155
Quoted 10 lines
src/perf/perf.cpp +5
-0free(name); name = strdup(event_name); + if (!name) { + fprintf(stderr, "failed to allocate event name\n"); + return; + } + char *c; c = strchr(name, ':');
Your patch as been merged.
Thank You
-C
Sergey Senozhatsky
Quoted 5 lines · Namhyung Kim
The third argument of PERF_EVENT_IOC_{ENABLE,DISABLE} ioctl is a flag to determine whether the command should be applied to group members. Since the powertop doesn't use the event grouping feature, it'd better passing 0 as a third argument explicitly.
[..]
<+629>: mov -0x88(%rbp),%rax <+636>: mov 0x8(%rax),%eax <+639>: mov $0x2400,%esi <+644>: mov %eax,%edi <+646>: mov $0x0,%eax <+651>: callq 0x404460 <ioctl(a)plt>
[..]
agree, let's pass 0 explicitly to perf_ioctl():
3307
3308 if (flags & PERF_IOC_FLAG_GROUP)
3309 perf_event_for_each(event, func);
3310 else
3311 perf_event_for_each_child(event, func);
3312
good catch, thank you!
-ss
Quoted 28 lines
Signed-off-by Namhyung Kim <namhyung(a)gmail.com>--- src/perf/perf.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)src/perf/perf.cpp +2
-2@@ -128,7 +128,7 @@ void perf_event::create_perf_event(char *eventname, int _cpu) return; } - ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE); + ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0); if (ret < 0) { fprintf(stderr, "failed to enable perf \n");@@ -210,7 +210,7 @@ void perf_event::start(void) void perf_event::stop(void) { int ret; - ret = ioctl(perf_fd, PERF_EVENT_IOC_DISABLE); + ret = ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0); if (ret) cout << "stop failing\n"; }
Chris Ferron
Quoted 24 lines · Namhyung Kim
The third argument of PERF_EVENT_IOC_{ENABLE,DISABLE} ioctl is a flag to determine whether the command should be applied to group members. Since the powertop doesn't use the event grouping feature, it'd better passing 0 as a third argument explicitly.
Signed-off-by Namhyung Kim <namhyung(a)gmail.com>--- src/perf/perf.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)src/perf/perf.cpp +1
-1@@ -128,7 +128,7 @@ void perf_event::create_perf_event(char *eventname, int _cpu) return; } - ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE); + ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0); if (ret < 0) { fprintf(stderr, "failed to enable perf \n");
src/perf/perf.cpp void perf_event::start(void) · line 216
Quoted 8 lines
src/perf/perf.cpp +1
-1@@ -210,7 +210,7 @@ void perf_event::start(void) void perf_event::stop(void) { int ret; - ret = ioctl(perf_fd, PERF_EVENT_IOC_DISABLE); + ret = ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0); if (ret) cout << "stop failing\n"; }
Your patch as been merged.
Thank You
-C