/* cpu-freq.c -- Continuous display of current time expressed in ISO 8601 and dev.cpu.0.freq expressed in MHz. Currently only useable on FreeBSD and possibly other BSDs. Copyright (C) 2012 Trond Endrestøl This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. $Ximalas$ */ //-------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include //-------------------------------------------------------------------- void interrupt(int s); //-------------------------------------------------------------------- int main(int argc, char **argv) { size_t len; int mib[4]; time_t local_time_t; struct tm *local_tm; char timebuffer[1024]; int freq; char freqbuffer[1024]; unsigned int sleeptime = 1; int i; //------------------------------------------------------------------ if (argc > 1) { errno = 0; sleeptime = (unsigned int)strtol(argv[1], NULL, 10); if (errno != 0 || sleeptime == 0) { if (errno != 0) { perror("strtol"); } // if fprintf(stderr, "Usage: %s [sleeptime-in-seconds]\n", argv[0]); return 1; } // if } // if //------------------------------------------------------------------ if (signal(SIGINT, interrupt) == SIG_ERR) { perror("signal"); return 1; } // if //------------------------------------------------------------------ // See cpufreq(4). // len = 4; if (sysctlnametomib("dev.cpu.0.freq", mib, &len) == -1) { perror("sysctlnametomib"); return 1; } // if while (1) { local_time_t = time(NULL); local_tm = localtime(&local_time_t); strftime(timebuffer, sizeof(timebuffer), "%FT%T%z: ", local_tm); len = sizeof(freq); if (sysctl(mib, 4, &freq, &len, NULL, 0) == -1) { perror("sysctl"); return 1; } // if else if (len > 0) { snprintf(freqbuffer, sizeof(freqbuffer), "%d MHz", freq); fputs(timebuffer, stdout); fputs(freqbuffer, stdout); fflush(stdout); sleep(sleeptime); fputc('\r', stdout); len = strlen(timebuffer) + strlen(freqbuffer); for (i = 0; i < len; i++) { fputc(' ', stdout); } // for fputc('\r', stdout); } // else if } // while /*NOTREACHED*/ return 0; } // main() //-------------------------------------------------------------------- void interrupt(int s) { fflush(stderr); fflush(stdout); fputc('\n', stdout); fflush(stdout); exit(0); } // interrupt() // cpu-freq.c