/** * Maintain profiles of a running stack. */ Profiler::Profiler(bool needCPUAffinity) : m_successful(true), m_stack(nullptr), m_frame_free_list(nullptr) { if (!s_rand_initialized) { s_rand_initialized = true; srand(math_generate_seed()); } if (needCPUAffinity) { // // Bind to a random cpu so that we can use rdtsc instruction. // int cur_cpu_id = rand() % s_machine.m_cpu_num; GET_AFFINITY(0, sizeof(cpu_set_t), &m_prev_mask); MachineInfo::BindToCPU(cur_cpu_id); m_MHz = s_machine.m_cpu_frequencies[cur_cpu_id]; } else { // // Take cpu0's speed as a proxy for all cpus. // m_MHz = s_machine.m_cpu_frequencies[0]; } memset(m_func_hash_counters, 0, sizeof(m_func_hash_counters)); }
Profiler() : m_successful(true), m_stack(NULL), m_frame_free_list(NULL) { if (!s_rand_initialized) { s_rand_initialized = true; srand(GENERATE_SEED()); } // bind to a random cpu so that we can use rdtsc instruction. int cur_cpu_id = rand() % s_machine.m_cpu_num; GET_AFFINITY(0, sizeof(cpu_set_t), &m_prev_mask); MachineInfo::BindToCPU(cur_cpu_id); m_MHz = s_machine.m_cpu_frequencies[cur_cpu_id]; memset(m_func_hash_counters, 0, sizeof(m_func_hash_counters)); }
MachineInfo() { m_cpu_num = sysconf(_SC_NPROCESSORS_CONF); m_cpu_frequencies = get_cpu_frequency_from_file("/proc/cpuinfo", m_cpu_num); if (m_cpu_frequencies) return; m_cpu_frequencies = new int64_t[m_cpu_num]; for (int i = 0; i < m_cpu_num; i++) { cpu_set_t prev_mask; GET_AFFINITY(0, sizeof(cpu_set_t), &prev_mask); BindToCPU(i); // Make sure the current process gets scheduled to the target cpu. This // might not be necessary though. usleep(0); m_cpu_frequencies[i] = get_cpu_frequency(); SET_AFFINITY(0, sizeof(cpu_set_t), &prev_mask); } }