From 86bbd14fe1585b5038d5e89ce30fdb43eefa3085 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Sat, 5 Feb 2022 15:05:31 -0500 Subject: [PATCH] Fixed memcpy attribution (now on specific lines, just like allocations). --- scalene/scalene_profiler.py | 28 +++++++++------------------- src/include/memcpysampler.hpp | 26 ++++++++++++++++++++------ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/scalene/scalene_profiler.py b/scalene/scalene_profiler.py index 620255bcd..202ed55e0 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -1219,29 +1219,19 @@ def memcpy_sigqueue_processor( with contextlib.suppress(ValueError): while Scalene.__memcpy_mapfile.read(): count_str = Scalene.__memcpy_mapfile.get_str() - (memcpy_time_str, count_str2, pid) = count_str.split(",") + (memcpy_time_str, count_str2, pid, filename, lineno, bytei) = count_str.split(",") if int(curr_pid) == int(pid): - arr.append((int(memcpy_time_str), int(count_str2))) + arr.append((filename, lineno, bytei, int(memcpy_time_str), int(count_str2))) arr.sort() - stats = Scalene.__stats - new_frames = Scalene.compute_frames_to_record(frame) - if not new_frames: - del frame - return - for item in arr: - _memcpy_time, count = item - for (the_frame, _tident, _orig_frame) in new_frames: - fname = Filename(the_frame.f_code.co_filename) - line_no = LineNumber(the_frame.f_lineno) - bytei = ByteCodeIndex(the_frame.f_lasti) - # Add the byte index to the set for this line. - stats.bytei_map[fname][line_no].add(bytei) - stats.memcpy_samples[fname][line_no] += count - del new_frames[:] - del new_frames - del frame + filename, lineno, byteindex, _memcpy_time, count = item + fname = Filename(filename) + line_no = int(LineNumber(lineno)) + bytei = ByteCodeIndex(byteindex) + # Add the byte index to the set for this line. + Scalene.__stats.bytei_map[fname][line_no].add(bytei) + Scalene.__stats.memcpy_samples[fname][line_no] += int(count) @staticmethod @lru_cache(None) diff --git a/src/include/memcpysampler.hpp b/src/include/memcpysampler.hpp index ebaaaaa89..00a32709e 100644 --- a/src/include/memcpysampler.hpp +++ b/src/include/memcpysampler.hpp @@ -189,7 +189,7 @@ class MemcpySampler { if (old_sig != SIG_DFL) signal(MemcpySignal, old_sig); init_lock.unlock(); auto pid = getpid(); - snprintf((char *)scalene_memcpy_signal_filename, FILENAME_LENGTH, fname, + snprintf_((char *)scalene_memcpy_signal_filename, BUFFER_LENGTH, fname, pid); // printf("initialized (%s)\n", scalene_memcpy_signal_filename); } @@ -279,13 +279,27 @@ class MemcpySampler { uint64_t _interval; uint64_t _memcpyOps; unsigned long long _memcpyTriggered; - static constexpr int FILENAME_LENGTH = 255; - char scalene_memcpy_signal_filename[FILENAME_LENGTH]; + static constexpr int BUFFER_LENGTH = 1024; + char scalene_memcpy_signal_filename[BUFFER_LENGTH]; void writeCount() { - char buf[FILENAME_LENGTH]; - snprintf(buf, FILENAME_LENGTH, "%d,%d,%d\n\n", _memcpyTriggered, _memcpyOps, - getpid()); +#if 1 + std::string filename; + int lineno = 1; + int bytei = 0; + decltype(whereInPython)* where = p_whereInPython; + if (where != nullptr && where(filename, lineno, bytei)) { + ; + } +#endif + char buf[BUFFER_LENGTH]; + snprintf_(buf, BUFFER_LENGTH, "%d,%d,%d,%s,%d,%d\n\n", + _memcpyTriggered, + _memcpyOps, + getpid(), + filename.c_str(), + lineno, + bytei); _samplefile.writeToFile(buf); } };