Skip to content

Commit

Permalink
Add a flag to display the CPU package temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker1024 committed Sep 14, 2020
1 parent fd24d28 commit 7743b50
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ coretemp
* `-F` Output temperature in Fahrenheit.
* `-c <num>` Specify which cores to report on, in a comma-separated list. If unspecified, reports all temperatures.
* `-r <num>` The accuracy of the temperature, in the number of decimal places. Defaults to 0.
* `-p` Display the CPU package temperature instead of the core temperatures.

## Maintainer

Expand Down
87 changes: 55 additions & 32 deletions smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,34 @@ void getCoreNumbers(char* arg, unsigned long* cores, char* errorMsg)
}
}

double convertToCorrectScale(char scale, double temperature) {
if (scale == 'F') {
return convertToFahrenheit(temperature);
} else {
return temperature;
}
}

void printTemperature(double temperature, unsigned int rounding) {
printf("%.*f\n", rounding, temperature);
}

enum OutputMode {
core,
package,
};

int main(int argc, char* argv[])
{
char scale = 'C';
bool specifiedCores = false;
unsigned long* coreList = malloc(sizeof(unsigned long));
unsigned long coreCount;
unsigned int rounding = 0;
enum OutputMode outputMode = core;

int argLabel;
while ((argLabel = getopt(argc, argv, "FCc:r:h")) != -1) {
while ((argLabel = getopt(argc, argv, "FCc:r:ph")) != -1) {
switch (argLabel) { // NOLINT(hicpp-multiway-paths-covered)
case 'F':
case 'C':
Expand All @@ -303,6 +321,9 @@ int main(int argc, char* argv[])
case 'r':
rounding = (int)parseNumArg(optarg, "Invalid decimal place limit.\n");
break;
case 'p':
outputMode = package;
break;
case 'h':
case '?':
printf("usage: coretemp <options>\n");
Expand All @@ -311,51 +332,53 @@ int main(int argc, char* argv[])
printf(" -C Display temperatures in degrees Celsius (Default).\n");
printf(" -c <num> Specify which cores to report on, in a comma-separated list. If unspecified, reports all temperatures.\n");
printf(" -r <num> The accuracy of the temperature, in the number of decimal places. Defaults to 0.\n");
printf(" -p Display the CPU package temperature instead of the core temperatures.\n");
printf(" -h Display this help.\n");
return -1;
}
}

if (!specifiedCores) {
coreCount = getPhysicalCoreCount();
coreList = realloc(coreList, coreCount * sizeof(unsigned long));
for (int i = 0; i < coreCount; ++i)
coreList[i] = i;
}

SMCOpen();

char templateKey[7];
double firstCoreTemperature = getTemperatureKeyTemplate(coreList[0], templateKey);
switch (outputMode) {
default:
case core: {
if (!specifiedCores) {
coreCount = getPhysicalCoreCount();
coreList = realloc(coreList, coreCount * sizeof(unsigned long));
for (int i = 0; i < coreCount; ++i)
coreList[i] = i;
}

if (firstCoreTemperature == 0) {
// The first core does not exist
printf("The specified core (%lu) does not exist.\n", coreList[0]);
exit(1);
}
char templateKey[7];
double firstCoreTemperature = getTemperatureKeyTemplate(coreList[0], templateKey);

if (scale == 'F') {
firstCoreTemperature = convertToFahrenheit(firstCoreTemperature);
}
if (firstCoreTemperature == 0) {
// The first core does not exist
printf("The specified core (%lu) does not exist.\n", coreList[0]);
exit(1);
}

printf("%.*f\n", rounding, firstCoreTemperature);
printTemperature(convertToCorrectScale(scale, firstCoreTemperature), rounding);

for (int i = 1; i < coreCount; ++i) {
char key[getTemperatureSMCKeySize(coreList[i])];
sprintf(key, templateKey, coreList[i]);
double temperature = SMCGetTemperature(key);
for (int i = 1; i < coreCount; ++i) {
char key[getTemperatureSMCKeySize(coreList[i])];
sprintf(key, templateKey, coreList[i]);
double temperature = SMCGetTemperature(key);

if (temperature == 0) {
// The specified core does not exist
printf("The specified core (%lu) does not exist.\n", coreList[i]);
exit(1);
}
if (temperature == 0) {
// The specified core does not exist
printf("The specified core (%lu) does not exist.\n", coreList[i]);
exit(1);
}

if (scale == 'F') {
temperature = convertToFahrenheit(temperature);
printTemperature(convertToCorrectScale(scale, temperature), rounding);
}
break;
}

printf("%.*f\n", rounding, temperature);
case package:
printTemperature(convertToCorrectScale(scale, SMCGetTemperature(SMC_CPU_PROXIMITY_TEMP)), rounding);
break;
}

SMCClose();
Expand Down
1 change: 1 addition & 0 deletions smc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define SMC_CPU_CORE_TEMP_PREFIX "TC"
#define SMC_CPU_CORE_TEMP_SUFFIX_OLD 'C'
#define SMC_CPU_CORE_TEMP_SUFFIX_NEW 'c'
#define SMC_CPU_PROXIMITY_TEMP "TC0P"

typedef struct {
char major;
Expand Down

0 comments on commit 7743b50

Please sign in to comment.