- * run func(cpu) on every cpu in /proc/stat
- * return max_cpu number
- */
-static int __must_check
-for_all_proc_cpus(int (func)(int))
-{
- FILE *fp;
- int cpu_num;
- int retval;
-
- fp = fopen(proc_stat, "r");
- if (!fp) {
- ERROR("%s: open failed", proc_stat);
- return -ERR_CANT_OPEN_FILE;
- }
-
- retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
- if (retval != 0) {
- ERROR("%s: failed to parse format", proc_stat);
- return -ERR_CANT_READ_PROC_STAT;
- }
-
- while (1) {
- retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
- if (retval != 1)
- break;
-
- retval = func(cpu_num);
- if (retval) {
- fclose(fp);
- return(retval);
- }
- }
- fclose(fp);
- return 0;
-}
-
-/*
- * count_cpus()
- * remember the last one seen, it will be the max
- */
-static int
-count_cpus(int cpu)
-{
- if (topo.max_cpu_num < cpu)
- topo.max_cpu_num = cpu;
-
- topo.num_cpus += 1;
- return 0;
-}
-static int
-mark_cpu_present(int cpu)
-{
- CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
- return 0;
-}
-
-
-static void
-turbostat_submit (const char *plugin_instance,
- const char *type, const char *type_instance,
- gauge_t value)
-{
- value_list_t vl = VALUE_LIST_INIT;
- value_t v;
-
- v.gauge = value;
- vl.values = &v;
- vl.values_len = 1;
- sstrncpy (vl.host, hostname_g, sizeof (vl.host));
- sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
- if (plugin_instance != NULL)
- sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
- sstrncpy (vl.type, type, sizeof (vl.type));
- if (type_instance != NULL)
- sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-
- plugin_dispatch_values (&vl);
-}
-
-/*
- * column formatting convention & formats
- * package: "pk" 2 columns %2d
- * core: "cor" 3 columns %3d
- * CPU: "CPU" 3 columns %3d
- * Pkg_W: %6.2
- * Cor_W: %6.2
- * GFX_W: %5.2
- * RAM_W: %5.2
- * GHz: "GHz" 3 columns %3.2
- * TSC: "TSC" 3 columns %3.2
- * SMI: "SMI" 4 columns %4d
- * percentage " %pc3" %6.2
- * Perf Status percentage: %5.2
- * "CTMP" 4 columns %4d
- */
-#define NAME_LEN 12
-static int
-submit_counters(struct thread_data *t, struct core_data *c,
- struct pkg_data *p)
-{
- char name[NAME_LEN];
- double interval_float;
-
- interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
-
- snprintf(name, NAME_LEN, "cpu%02d", t->cpu_id);
-
- if (do_nhm_cstates) {
- if (!skip_c0)
- turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
- if (!skip_c1)
- turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
- }
-
- /* GHz */
- if (has_aperf && ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc))))
- turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
-
- /* SMI */
- if (do_smi)
- turbostat_submit(NULL, "current", name, t->smi_count);
-
- /* print per-core data only for 1st thread in core */
- if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
- goto done;
-
- snprintf(name, NAME_LEN, "core%02d", c->core_id);
-
- if (do_nhm_cstates && !do_slm_cstates)
- turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
- if (do_nhm_cstates)
- turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
- if (do_snb_cstates)
- turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
-
- if (do_dts)
- turbostat_submit(NULL, "temperature", name, c->core_temp_c);
-
- /* print per-package data only for 1st core in package */
- if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
- goto done;
-
- snprintf(name, NAME_LEN, "pc%02d", p->package_id);
-
- if (do_ptm)
- turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
-
- if (do_snb_cstates)
- turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
- if (do_nhm_cstates && !do_slm_cstates)
- turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
- if (do_nhm_cstates && !do_slm_cstates)
- turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
- if (do_snb_cstates)
- turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
- if (do_c8_c9_c10) {
- turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
- turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
- turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
- }
-
- if (do_rapl) {
- if (do_rapl & RAPL_PKG)
- turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
- if (do_rapl & RAPL_CORES)
- turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
- if (do_rapl & RAPL_GFX)
- turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
- if (do_rapl & RAPL_DRAM)
- turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
- }
-done:
- return 0;
-}
-
-static int
-turbostat_read(user_data_t * not_used)
-{
- int ret;
-
- if (!allocated) {
- if ((ret = setup_all_buffers()) < 0)
- return ret;
- }
-
- if (for_all_proc_cpus(cpu_is_not_present)) {
- free_all_buffers();
- if ((ret = setup_all_buffers()) < 0)
- return ret;
- if (for_all_proc_cpus(cpu_is_not_present))
- return -ERR_CPU_NOT_PRESENT;
- }
-
- if (!initialized) {
- if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
- return ret;
- gettimeofday(&tv_even, (struct timezone *)NULL);
- is_even = true;
- initialized = true;
- return 0;
- }
-
- if (is_even) {
- if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
- return ret;
- gettimeofday(&tv_odd, (struct timezone *)NULL);
- is_even = false;
- timersub(&tv_odd, &tv_even, &tv_delta);
- if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
- return ret;
- if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
- return ret;
- } else {
- if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
- return ret;
- gettimeofday(&tv_even, (struct timezone *)NULL);
- is_even = true;
- timersub(&tv_even, &tv_odd, &tv_delta);
- if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
- return ret;
- if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
- return ret;
- }
- return 0;
-}
-
-static int __must_check
-check_dev_msr()
-{
- struct stat sb;
-
- if (stat("/dev/cpu/0/msr", &sb)) {
- ERROR("no /dev/cpu/0/msr\n"
- "Try \"# modprobe msr\"");
- return -ERR_NO_MSR;
- }
- return 0;
-}
-
-static int __must_check
-check_super_user()
-{
- if (getuid() != 0) {
- ERROR("must be root");
- return -ERR_NOT_ROOT;
- }
- return 0;
-}
-
-
-#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
-#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
-
-static double
-get_tdp(unsigned int model)
-{
- unsigned long long msr;
-
- if (do_rapl & RAPL_PKG_POWER_INFO)
- if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
- return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
-
- switch (model) {
- case 0x37:
- case 0x4D:
- return 30.0;
- default:
- return 135.0;
- }
-}
-
-
-/*
- * rapl_probe()