[PATCH] Correctly track the size of operf poll_data array
William Cohen <[email protected]>
| Newsgroups | gmane.linux.oprofile |
|---|---|
| Message-ID | <[email protected]> |
The new[] operator in C++ returns a pointer to a block of memory allocated for an array of objects. If the sizeof operator is used on the value returned by new[], the sizeof operator will return the space required for the pointer rather than the space required for the array. Thus, the following code will not correctly count the number of elements in the array: struct pollfd * poll_data; poll_data = new struct pollfd [100]; num_mmaps = sizeof(poll_data)/sizeof(poll_data[0]); To properly track the size of the dynamically allocated poll_data array a num_mmaps field has been added to the operf_record class. Signed-off-by: William Cohen <[email protected]> --- libperf_events/operf_counter.cpp | 5 ++--- libperf_events/operf_counter.h | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libperf_events/operf_counter.cpp b/libperf_events/operf_counter.cpp index 42c0cd1..d5fe8a5 100644 --- a/libperf_events/operf_counter.cpp +++ b/libperf_events/operf_counter.cpp @@ -336,6 +336,7 @@ operf_record::operf_record(int out_fd, bool sys_wide, pid_t the_pid, bool pid_ru evts = events; valid = false; poll_data = NULL; + num_mmaps = 0; output_fd = out_fd; read_comm_pipe = _convert_read_pipe; write_comm_pipe = _convert_write_pipe; @@ -705,7 +706,6 @@ void operf_record::setup() } } } - int num_mmaps; if (pid_started && (procs.size() > 1)) num_mmaps = procs.size(); else @@ -764,12 +764,11 @@ void operf_record::record_process_info(void) int operf_record::_start_recoding_new_thread(pid_t id) { string err_msg; - int num_mmaps, rc, fd_for_set_output = -1; + int rc, fd_for_set_output = -1; struct comm_event ce; u64 sample_id; struct pollfd * old_polldata = poll_data; - num_mmaps = sizeof(poll_data)/sizeof(poll_data[0]); num_mmaps++; poll_data = new struct pollfd [num_mmaps]; // Copy only the existing pollfd objects from the array. The new pollfd will diff --git a/libperf_events/operf_counter.h b/libperf_events/operf_counter.h index 4eb7775..6e19da9 100644 --- a/libperf_events/operf_counter.h +++ b/libperf_events/operf_counter.h @@ -111,6 +111,7 @@ private: // Array of size 'num_cpus_used_for_perf_event_open * num_pids * num_events' struct pollfd * poll_data; std::vector<struct mmap_data> samples_array; + int num_mmaps; int num_cpus; pid_t pid_to_profile; /* When doing --pid or --system-wide profiling, we'll obtain process information -- 2.1.0 ------------------------------------------------------------------------------ One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y