Code

Turbostat: Re-order code and add comments
authorVincent Brillault <git@lerya.net>
Sat, 20 Sep 2014 07:30:47 +0000 (09:30 +0200)
committerVincent Brillault <git@lerya.net>
Sat, 28 Feb 2015 06:35:41 +0000 (07:35 +0100)
No algorithmic change in this commit

src/turbostat.c

index ac74f52259ec474a10af63e20700fa916f587a63..f7b0b1d9261f97a0cac044342761f7b6a4004471 100644 (file)
@@ -255,48 +255,10 @@ enum return_values {
        UNSUPPORTED_CPU,
 };
 
-static int setup_all_buffers(void);
-
-static int
-cpu_is_not_present(int cpu)
-{
-       return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
-}
-/*
- * run func(thread, core, package) in topology order
- * skip non-present cpus
- */
-
-static int __attribute__((warn_unused_result))
-for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
-       struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
-{
-       int retval, pkg_no, core_no, thread_no;
-
-       for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
-               for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
-                       for (thread_no = 0; thread_no <
-                               topo.num_threads_per_core; ++thread_no) {
-                               struct thread_data *t;
-                               struct core_data *c;
-                               struct pkg_data *p;
-
-                               t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
 
-                               if (cpu_is_not_present(t->cpu_id))
-                                       continue;
-
-                               c = GET_CORE(core_base, core_no, pkg_no);
-                               p = GET_PKG(pkg_base, pkg_no);
-
-                               retval = func(t, c, p);
-                               if (retval)
-                                       return retval;
-                       }
-               }
-       }
-       return 0;
-}
+/*****************************
+ *  MSR Manipulation helpers *
+ *****************************/
 
 /*
  * Open a MSR device for reading
@@ -366,6 +328,128 @@ get_msr(int cpu, off_t offset, unsigned long long *msr)
        return retval;
 }
 
+
+/********************************
+ * Raw data acquisition (1 CPU) *
+ ********************************/
+
+/*
+ * Read every data avalaible for a single CPU
+ *
+ * Core data is shared for all threads in one core: extracted only for the first thread
+ * Package data is shared for all core in one package: extracted only for the first thread of the first core
+ *
+ * Side effect: migrates to the targeted CPU
+ */
+static int __attribute__((warn_unused_result))
+get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
+{
+       int cpu = t->cpu_id;
+       unsigned long long msr;
+       int msr_fd;
+       int retval = 0;
+
+       msr_fd = open_msr(cpu, 1);
+       if (msr_fd < 0)
+               return msr_fd;
+
+#define READ_MSR(msr, dst)                     \
+do {                                           \
+       if (read_msr(msr_fd, msr, dst)) {       \
+               retval = -ERR_##msr;            \
+               goto out;                       \
+       }                                       \
+} while (0)
+
+       READ_MSR(MSR_IA32_TSC, &t->tsc);
+
+       READ_MSR(MSR_IA32_APERF, &t->aperf);
+       READ_MSR(MSR_IA32_MPERF, &t->mperf);
+
+       READ_MSR(MSR_SMI_COUNT, &msr);
+       t->smi_count = msr & 0xFFFFFFFF;
+
+       /* collect core counters only for 1st thread in core */
+       if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
+               retval = 0;
+               goto out;
+       }
+
+       if (do_core_cstate & (1 << 3))
+               READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
+       if (do_core_cstate & (1 << 6))
+               READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
+       if (do_core_cstate & (1 << 7))
+               READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
+
+       if (do_dts) {
+               READ_MSR(MSR_IA32_THERM_STATUS, &msr);
+               c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
+       }
+
+       /* collect package counters only for 1st core in package */
+       if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
+               retval = 0;
+               goto out;
+       }
+
+        if (do_pkg_cstate & (1 << 2))
+                READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
+        if (do_pkg_cstate & (1 << 3))
+                READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
+        if (do_pkg_cstate & (1 << 6))
+                READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
+        if (do_pkg_cstate & (1 << 7))
+                READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
+        if (do_pkg_cstate & (1 << 8))
+                READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
+        if (do_pkg_cstate & (1 << 9))
+                READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
+        if (do_pkg_cstate & (1 << 10))
+                READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
+
+       if (do_rapl & RAPL_PKG) {
+               READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
+               p->energy_pkg = msr & 0xFFFFFFFF;
+       }
+       if (do_rapl & RAPL_CORES) {
+               READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
+               p->energy_cores = msr & 0xFFFFFFFF;
+       }
+       if (do_rapl & RAPL_DRAM) {
+               READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
+               p->energy_dram = msr & 0xFFFFFFFF;
+       }
+       if (do_rapl & RAPL_GFX) {
+               READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
+               p->energy_gfx = msr & 0xFFFFFFFF;
+       }
+       if (do_rapl & RAPL_PKG_PERF_STATUS) {
+               READ_MSR(MSR_PKG_PERF_STATUS, &msr);
+               p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
+       }
+       if (do_rapl & RAPL_DRAM_PERF_STATUS) {
+               READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
+               p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
+       }
+       if (do_ptm) {
+               READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
+               p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
+       }
+
+out:
+       close(msr_fd);
+       return retval;
+}
+
+
+/**********************************
+ * Evaluating the changes (1 CPU) *
+ **********************************/
+
+/*
+ * Do delta = new - old on 32bits cyclique intergers
+ */
 #define DELTA_WRAP32(delta, new, old)                  \
        if (new > old) {                                \
                delta = new - old;                      \
@@ -466,112 +550,234 @@ delta_thread(struct thread_data *delta, const struct thread_data *new, const str
        return 0;
 }
 
+/**********************************
+ * Submitting the results (1 CPU) *
+ **********************************/
+
+/*
+ * Submit one gauge value
+ */
+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);
+}
+
+/*
+ * Submit every data for a single CPU
+ *
+ * Core data is shared for all threads in one core: submitted only for the first thread
+ * Package data is shared for all core in one package: submitted only for the first thread of the first core
+ */
+static int
+submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
+{
+       char name[12];
+       double interval_float;
+
+       interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
+
+       ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
+
+       if (!aperf_mperf_unstable)
+               turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
+       if (!aperf_mperf_unstable)
+               turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
+
+       /* GHz */
+       if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
+               turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
+
+       /* SMI */
+       turbostat_submit(NULL, "current", name, t->smi_count);
+
+       /* submit per-core data only for 1st thread in core */
+       if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
+               goto done;
+
+       ssnprintf(name, sizeof(name), "core%02d", c->core_id);
+
+       if (do_core_cstate & (1 << 3))
+               turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
+       if (do_core_cstate & (1 << 6))
+               turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
+       if (do_core_cstate & (1 << 7))
+               turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
+
+       if (do_dts)
+               turbostat_submit(NULL, "temperature", name, c->core_temp_c);
+
+       /* submit per-package data only for 1st core in package */
+       if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
+               goto done;
+
+       ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
+
+       if (do_ptm)
+               turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
+
+       if (do_pkg_cstate & (1 << 2))
+               turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
+       if (do_pkg_cstate & (1 << 3))
+               turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
+       if (do_pkg_cstate & (1 << 6))
+               turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
+       if (do_pkg_cstate & (1 << 7))
+               turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
+       if (do_pkg_cstate & (1 << 8))
+               turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
+       if (do_pkg_cstate & (1 << 9))
+               turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
+       if (do_pkg_cstate & (1 << 10))
+               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;
+}
+
+/**********************************
+ * Looping function over all CPUs *
+ **********************************/
+
 /*
- * get_counters(...)
- * migrate to cpu
- * acquire and record local counters for that cpu
+ * Check if a given cpu id is in our compiled list of existing CPUs
+ *
+ * CHECKME: Why do we need this?
+ */
+static int
+cpu_is_not_present(int cpu)
+{
+       return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
+}
+
+/*
+ * Loop on all CPUs in topological order
+ *
+ * Skip 'non-present' cpus (CHECKME: Why do we need this?)
+ * Return the error code at the first error or 0
  */
 static int __attribute__((warn_unused_result))
-get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
+for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
+       struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
 {
-       int cpu = t->cpu_id;
-       unsigned long long msr;
-       int msr_fd;
-       int retval = 0;
+       int retval, pkg_no, core_no, thread_no;
 
-       msr_fd = open_msr(cpu, 1);
-       if (msr_fd < 0)
-               return msr_fd;
+       for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
+               for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
+                       for (thread_no = 0; thread_no <
+                               topo.num_threads_per_core; ++thread_no) {
+                               struct thread_data *t;
+                               struct core_data *c;
+                               struct pkg_data *p;
 
-#define READ_MSR(msr, dst)                     \
-do {                                           \
-       if (read_msr(msr_fd, msr, dst)) {       \
-               retval = -ERR_##msr;            \
-               goto out;                       \
-       }                                       \
-} while (0)
+                               t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
 
-       READ_MSR(MSR_IA32_TSC, &t->tsc);
+                               if (cpu_is_not_present(t->cpu_id))
+                                       continue;
+
+                               c = GET_CORE(core_base, core_no, pkg_no);
+                               p = GET_PKG(pkg_base, pkg_no);
+
+                               retval = func(t, c, p);
+                               if (retval)
+                                       return retval;
+                       }
+               }
+       }
+       return 0;
+}
+
+/*
+ * Dedicated loop: Extract every data evolution for all CPU
+ *
+ * Core data is shared for all threads in one core: extracted only for the first thread
+ * Package data is shared for all core in one package: extracted only for the first thread of the first core
+ */
+static int __attribute__((warn_unused_result))
+for_all_cpus_delta(const struct thread_data *thread_new_base, const struct core_data *core_new_base, const struct pkg_data *pkg_new_base,
+                  const struct thread_data *thread_old_base, const struct core_data *core_old_base, const struct pkg_data *pkg_old_base)
+{
+       int retval, pkg_no, core_no, thread_no;
+
+       for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
+               for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
+                       for (thread_no = 0; thread_no <
+                               topo.num_threads_per_core; ++thread_no) {
+                               struct thread_data *t_delta;
+                               const struct thread_data *t_old, *t_new;
+                               struct core_data *c_delta;
+
+                               /* Get correct pointers for threads */
+                               t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
+                               t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
+                               t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
+
+                               /* Skip threads that disappeared */
+                               if (cpu_is_not_present(t_delta->cpu_id))
+                                       continue;
 
-       READ_MSR(MSR_IA32_APERF, &t->aperf);
-       READ_MSR(MSR_IA32_MPERF, &t->mperf);
+                               /* c_delta is always required for delta_thread */
+                               c_delta = GET_CORE(core_delta, core_no, pkg_no);
 
-       READ_MSR(MSR_SMI_COUNT, &msr);
-       t->smi_count = msr & 0xFFFFFFFF;
+                               /* calculate core delta only for 1st thread in core */
+                               if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE) {
+                                       const struct core_data *c_old, *c_new;
 
-       /* collect core counters only for 1st thread in core */
-       if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
-               retval = 0;
-               goto out;
-       }
+                                       c_new = GET_CORE(core_new_base, core_no, pkg_no);
+                                       c_old = GET_CORE(core_old_base, core_no, pkg_no);
 
-       if (do_core_cstate & (1 << 3))
-               READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
-       if (do_core_cstate & (1 << 6))
-               READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
-       if (do_core_cstate & (1 << 7))
-               READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
+                                       delta_core(c_delta, c_new, c_old);
+                               }
 
-       if (do_dts) {
-               READ_MSR(MSR_IA32_THERM_STATUS, &msr);
-               c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
-       }
+                               /* Always calculate thread delta */
+                               retval = delta_thread(t_delta, t_new, t_old, c_delta);
+                               if (retval)
+                                       return retval;
 
-       /* collect package counters only for 1st core in package */
-       if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
-               retval = 0;
-               goto out;
-       }
+                               /* calculate package delta only for 1st core in package */
+                               if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) {
+                                       struct pkg_data *p_delta;
+                                       const struct pkg_data *p_old, *p_new;
 
-        if (do_pkg_cstate & (1 << 2))
-                READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
-        if (do_pkg_cstate & (1 << 3))
-                READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
-        if (do_pkg_cstate & (1 << 6))
-                READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
-        if (do_pkg_cstate & (1 << 7))
-                READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
-        if (do_pkg_cstate & (1 << 8))
-                READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
-        if (do_pkg_cstate & (1 << 9))
-                READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
-        if (do_pkg_cstate & (1 << 10))
-                READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
+                                       p_delta = GET_PKG(package_delta, pkg_no);
+                                       p_new = GET_PKG(pkg_new_base, pkg_no);
+                                       p_old = GET_PKG(pkg_old_base, pkg_no);
 
-       if (do_rapl & RAPL_PKG) {
-               READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
-               p->energy_pkg = msr & 0xFFFFFFFF;
-       }
-       if (do_rapl & RAPL_CORES) {
-               READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
-               p->energy_cores = msr & 0xFFFFFFFF;
-       }
-       if (do_rapl & RAPL_DRAM) {
-               READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
-               p->energy_dram = msr & 0xFFFFFFFF;
-       }
-       if (do_rapl & RAPL_GFX) {
-               READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
-               p->energy_gfx = msr & 0xFFFFFFFF;
-       }
-       if (do_rapl & RAPL_PKG_PERF_STATUS) {
-               READ_MSR(MSR_PKG_PERF_STATUS, &msr);
-               p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
-       }
-       if (do_rapl & RAPL_DRAM_PERF_STATUS) {
-               READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
-               p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
-       }
-       if (do_ptm) {
-               READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
-               p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
+                                       delta_package(p_delta, p_new, p_old);
+                               }
+                       }
+               }
        }
-
-out:
-       close(msr_fd);
-       return retval;
+       return 0;
 }
 
+
 static void
 free_all_buffers(void)
 {
@@ -704,70 +910,6 @@ get_num_ht_siblings(int cpu)
                return 1;
 }
 
-/*
- * Extract every data evolution for all CPU
- *
- * Core data is shared for all threads in one core: extracted only for the first thread
- * Package data is shared for all core in one package: extracted only for the first thread of the first core
- */
-static int __attribute__((warn_unused_result))
-for_all_cpus_delta(const struct thread_data *thread_new_base, const struct core_data *core_new_base, const struct pkg_data *pkg_new_base,
-                  const struct thread_data *thread_old_base, const struct core_data *core_old_base, const struct pkg_data *pkg_old_base)
-{
-       int retval, pkg_no, core_no, thread_no;
-
-       for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
-               for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
-                       for (thread_no = 0; thread_no <
-                               topo.num_threads_per_core; ++thread_no) {
-                               struct thread_data *t_delta;
-                               const struct thread_data *t_old, *t_new;
-                               struct core_data *c_delta;
-
-                               /* Get correct pointers for threads */
-                               t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
-                               t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
-                               t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
-
-                               /* Skip threads that disappeared */
-                               if (cpu_is_not_present(t_delta->cpu_id))
-                                       continue;
-
-                               /* c_delta is always required for delta_thread */
-                               c_delta = GET_CORE(core_delta, core_no, pkg_no);
-
-                               /* calculate core delta only for 1st thread in core */
-                               if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE) {
-                                       const struct core_data *c_old, *c_new;
-
-                                       c_new = GET_CORE(core_new_base, core_no, pkg_no);
-                                       c_old = GET_CORE(core_old_base, core_no, pkg_no);
-
-                                       delta_core(c_delta, c_new, c_old);
-                               }
-
-                               /* Always calculate thread delta */
-                               retval = delta_thread(t_delta, t_new, t_old, c_delta);
-                               if (retval)
-                                       return retval;
-
-                               /* calculate package delta only for 1st core in package */
-                               if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) {
-                                       struct pkg_data *p_delta;
-                                       const struct pkg_data *p_old, *p_new;
-
-                                       p_delta = GET_PKG(package_delta, pkg_no);
-                                       p_new = GET_PKG(pkg_new_base, pkg_no);
-                                       p_old = GET_PKG(pkg_old_base, pkg_no);
-
-                                       delta_package(p_delta, p_new, p_old);
-                               }
-                       }
-               }
-       }
-       return 0;
-}
-
 /*
  * run func(cpu) on every cpu in /proc/stat
  * return max_cpu number
@@ -828,119 +970,7 @@ mark_cpu_present(int cpu)
 }
 
 
-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
- */
-static int
-submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
-{
-       char name[12];
-       double interval_float;
-
-       interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
-
-       ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
-
-       if (!aperf_mperf_unstable)
-               turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
-       if (!aperf_mperf_unstable)
-               turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
-
-       /* GHz */
-       if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
-               turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
-
-       /* 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;
-
-       ssnprintf(name, sizeof(name), "core%02d", c->core_id);
-
-       if (do_core_cstate & (1 << 3))
-               turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
-       if (do_core_cstate & (1 << 6))
-               turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
-       if (do_core_cstate & (1 << 7))
-               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;
-
-       ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
-
-       if (do_ptm)
-               turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
-
-       if (do_pkg_cstate & (1 << 2))
-               turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
-       if (do_pkg_cstate & (1 << 3))
-               turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
-       if (do_pkg_cstate & (1 << 6))
-               turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
-       if (do_pkg_cstate & (1 << 7))
-               turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
-       if (do_pkg_cstate & (1 << 8))
-               turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
-       if (do_pkg_cstate & (1 << 9))
-               turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
-       if (do_pkg_cstate & (1 << 10))
-               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 setup_all_buffers(void);
 
 static int
 turbostat_read(user_data_t * not_used)