Turbostat: use ssnprintf instead of snprintf
[collectd.git] / src / turbostat.c
index c777860..8c7af14 100644 (file)
@@ -102,7 +102,6 @@ static double rapl_energy_units;
 
 int aperf_mperf_unstable;
 int backwards_count;
-char *progname;
 
 cpu_set_t *cpu_present_set, *cpu_affinity_set;
 size_t cpu_present_setsize, cpu_affinity_setsize;
@@ -270,9 +269,19 @@ static int __attribute__((warn_unused_result))
 open_msr(int cpu)
 {
        char pathname[32];
+       int fd;
+
+       /* FIXME: Do we really need this, why? */
+       if (cpu_migrate(cpu)) {
+               ERROR("Could not migrate to CPU %d", cpu);
+               return -ERR_CPU_MIGRATE;
+       }
 
-       ssnprintf(pathname, 32, "/dev/cpu/%d/msr", cpu);
-       return open(pathname, O_RDONLY);
+       ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
+       fd = open(pathname, O_RDONLY);
+       if (fd < 0)
+               return -ERR_CANT_OPEN_MSR;
+       return fd;
 }
 
 static int __attribute__((warn_unused_result))
@@ -283,7 +292,7 @@ read_msr(int fd, off_t offset, unsigned long long *msr)
        retval = pread(fd, msr, sizeof *msr, offset);
 
        if (retval != sizeof *msr) {
-               ERROR ("MSR offset 0x%llx read failed", (unsigned long long)offset);
+               ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
                return -1;
        }
        return 0;
@@ -297,7 +306,7 @@ get_msr(int cpu, off_t offset, unsigned long long *msr)
 
        fd = open_msr(cpu);
        if (fd < 0)
-               return -1;
+               return fd;
        retval = read_msr(fd, offset, msr);
        close(fd);
        return retval;
@@ -350,9 +359,9 @@ delta_thread(struct thread_data *new, struct thread_data *old,
 
        /* check for TSC < 1 Mcycles over interval */
        if (old->tsc < (1000 * 1000)) {
-               WARNING("Insanely slow TSC rate, TSC stops in idle?\n"
-                       "You can disable all c-states by booting with \"idle=poll\"\n"
-                       "or just the deep ones with \"processor.max_cstate=1\"");
+               WARNING("Insanely slow TSC rate, TSC stops in idle? ");
+               WARNING("You can disable all c-states by booting with \"idle=poll\" ");
+               WARNING("or just the deep ones with \"processor.max_cstate=1\"");
                return -1;
        }
 
@@ -364,9 +373,9 @@ delta_thread(struct thread_data *new, struct thread_data *old,
        } else {
 
                if (!aperf_mperf_unstable) {
-                       WARNING("%s: APERF or MPERF went backwards *\n", progname);
-                       WARNING("* Frequency results do not cover entire interval *\n");
-                       WARNING("* fix this by running Linux-2.6.30 or later *\n");
+                       WARNING(" APERF or MPERF went backwards * ");
+                       WARNING("* Frequency results do not cover entire interval *");
+                       WARNING("* fix this by running Linux-2.6.30 or later *");
 
                        aperf_mperf_unstable = 1;
                }
@@ -393,7 +402,7 @@ delta_thread(struct thread_data *new, struct thread_data *old,
        }
 
        if (old->mperf == 0) {
-               WARNING("cpu%d MPERF 0!\n", old->cpu_id);
+               WARNING("cpu%d MPERF 0!", old->cpu_id);
                old->mperf = 1; /* divide by 0 protection */
        }
 
@@ -438,14 +447,9 @@ get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
        int msr_fd;
        int retval = 0;
 
-       if (cpu_migrate(cpu)) {
-               WARNING("Could not migrate to CPU %d\n", cpu);
-               return -ERR_CPU_MIGRATE;
-       }
-
        msr_fd = open_msr(cpu);
        if (msr_fd < 0)
-               return -ERR_CANT_OPEN_MSR;
+               return msr_fd;
 
 #define READ_MSR(msr, dst)                     \
 do {                                           \
@@ -635,7 +639,7 @@ get_num_ht_siblings(int cpu)
        int matches;
        char character;
 
-       ssnprintf(path, 80, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
+       ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
        filep = fopen(path, "r");
         if (!filep) {
                 ERROR("%s: open failed", path);
@@ -809,7 +813,7 @@ submit_counters(struct thread_data *t, struct core_data *c,
 
        interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
 
-       snprintf(name, NAME_LEN, "cpu%02d", t->cpu_id);
+       ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
 
        if (!skip_c0)
                turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
@@ -827,7 +831,7 @@ submit_counters(struct thread_data *t, struct core_data *c,
        if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
                goto done;
 
-       snprintf(name, NAME_LEN, "core%02d", c->core_id);
+       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);
@@ -843,7 +847,7 @@ submit_counters(struct thread_data *t, struct core_data *c,
        if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
                goto done;
 
-       snprintf(name, NAME_LEN, "pkg%02d", p->package_id);
+       ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
 
        if (do_ptm)
                turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
@@ -934,8 +938,7 @@ check_dev_msr()
        struct stat sb;
 
        if (stat("/dev/cpu/0/msr", &sb)) {
-               ERROR("no /dev/cpu/0/msr\n"
-                       "Try \"# modprobe msr\"");
+               ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
                return -ERR_NO_MSR;
        }
        return 0;
@@ -968,7 +971,6 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
 {
        unsigned long long msr;
        unsigned int target_c_local;
-       int cpu;
 
        /* tcc_activation_temp is used only for dts or ptm */
        if (!(do_dts || do_ptm))
@@ -978,12 +980,6 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
        if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
                return 0;
 
-       cpu = t->cpu_id;
-       if (cpu_migrate(cpu)) {
-               ERROR("Could not migrate to CPU %d\n", cpu);
-               return -ERR_CPU_MIGRATE;
-       }
-
        if (tcc_activation_temp_override != 0) {
                tcc_activation_temp = tcc_activation_temp_override;
                ERROR("cpu%d: Using cmdline TCC Target (%d C)\n",
@@ -991,7 +987,7 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
                return 0;
        }
 
-       if (get_msr(cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
+       if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
                goto guess;
 
        target_c_local = (msr >> 16) & 0x7F;
@@ -1005,8 +1001,8 @@ set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_da
 
 guess:
        tcc_activation_temp = TJMAX_DEFAULT;
-       WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
-               cpu, tcc_activation_temp);
+       WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify",
+               t->cpu_id, tcc_activation_temp);
 
        return 0;
 }
@@ -1283,8 +1279,7 @@ topology_probe()
                int siblings;
 
                if (cpu_is_not_present(i)) {
-                       //if (verbose > 1)
-                               fprintf(stderr, "cpu%d NOT PRESENT\n", i);
+                       WARNING("cpu%d NOT PRESENT", i);
                        continue;
                }
                cpus[i].core_id = get_core_id(i);