diff --git a/llvm/projects/gpu_profiler/include/profiler.h b/llvm/projects/gpu_profiler/include/profiler.h index b776ed2b6642ee773783e48c9ba408d33d211f43..78c0f18071e916edfff435844fd990936855c410 100644 --- a/llvm/projects/gpu_profiler/include/profiler.h +++ b/llvm/projects/gpu_profiler/include/profiler.h @@ -65,6 +65,11 @@ private: const std::string ddr_power_rail = "/sys/devices/3160000.i2c/i2c-0/0-0041/iio_device/in_power2_input"; const std::string soc_power_rail = "/sys/devices/3160000.i2c/i2c-0/0-0040/iio_device/in_power1_input"; const std::string sys_power_rail = "/sys/devices/3160000.i2c/i2c-0/0-0041/iio_device/in_power0_input"; + // Critical assumption: If this file doesn't exist, then the board isn't a Jetson + const std::string jetson_chip_id = "/sys/module/tegra_fuse/parameters/tegra_chip_id"; + + // True if running on Jetson, else false + bool on_jetson_; // An individual power reading struct PowerReading { diff --git a/llvm/projects/gpu_profiler/src/profiler.cpp b/llvm/projects/gpu_profiler/src/profiler.cpp index 188223a9059eede6d2a32e853dee22b95ecb719e..822d708d58f6c8468cb4b84ed23e988251b95159 100644 --- a/llvm/projects/gpu_profiler/src/profiler.cpp +++ b/llvm/projects/gpu_profiler/src/profiler.cpp @@ -1,4 +1,4 @@ -#include "profiler.h" +#include "profiler.h" Profiler::Profiler() : should_run_profiler_(false), should_stop_profiler_(false) { // Open all streams. Not done in start_profiler() function bc the streams @@ -9,8 +9,14 @@ Profiler::Profiler() : should_run_profiler_(false), should_stop_profiler_(false) soc_stream_.open(soc_power_rail, std::ifstream::in); sys_stream_.open(sys_power_rail, std::ifstream::in); - if (!cpu_stream_.is_open() || !gpu_stream_.is_open() || !ddr_stream_.is_open() - || !soc_stream_.is_open() || !sys_stream_.is_open()) { + // Check if the jetson file id file exists to indirectly check architecture + std::ifstream jetson_file(jetson_chip_id); + on_jetson_ = jetson_file.good(); + + if (on_jetson_ && + (!cpu_stream_.is_open() || !gpu_stream_.is_open() + || !ddr_stream_.is_open() || !soc_stream_.is_open() + || !sys_stream_.is_open())) { std::cout << "Failed to open one of the power rails for reading\n"; exit(1); } @@ -68,16 +74,23 @@ void Profiler::pause_profiler() { // Returns this as a pair of <delta time in milliseconds, energy> std::pair<double, double> Profiler::get_time_energy() const { double total_energy = 0.0; - - std::chrono::time_point<std::chrono::high_resolution_clock> prev_time = start_time_; - for (auto reading : power_readings_) { - std::chrono::duration<double> duration = reading.time_ - prev_time; - total_energy += reading.gpu_ * duration.count(); - total_energy += reading.ddr_ * duration.count(); - prev_time = reading.time_; + double delta_time = 0.0; + + if (on_jetson_) { + std::chrono::time_point<std::chrono::high_resolution_clock> prev_time = start_time_; + for (auto reading : power_readings_) { + std::chrono::duration<double> duration = reading.time_ - prev_time; + total_energy += reading.gpu_ * duration.count(); + total_energy += reading.ddr_ * duration.count(); + prev_time = reading.time_; + } + delta_time = std::chrono::duration<double, std::milli>(prev_time + - start_time_).count(); + } else { + auto last_reading_time = power_readings_[power_readings_.size() - 1].time_; + delta_time = std::chrono::duration<double, std::milli>(last_reading_time + - start_time_).count(); } - double delta_time = std::chrono::duration<double, std::milli>(prev_time - - start_time_).count(); return std::make_pair(delta_time, total_energy); }