Go Back   nV News Forums > Linux Support Forums > NVIDIA Linux

Newegg Daily Deals

Reply
 
Thread Tools
Old 02-17-11, 08:34 AM   #1
sescandor
Registered User
 
Join Date: Feb 2011
Posts: 3
Default nVidia API for GPU temperature?

I would like to obtain the GPU temperature for the Quadro FX 1800 through C code in Linux. The constraint that I have is that I can't use libraries that are GPL licensed.

What I know so far:
* I notice that there is an NVAPI dll for Windows, but I haven't seen an equivalent for Linux.
* I also notice that nvidia-settings uses a library called libXNVCtrl in order to query the driver for the GPU temperature, but libXNVCtrl is GPL licensed, so I can't use it in my code.
* I also have a constraint that I can't fork another process that runs the command for nvidia-settings, and then pipe the data from the output of nvidia-settings to the process that is running my code.

Is there another way (much like in Windows) to obtain the GPU temperature without linking in the libXNVCtrl GPL library into my code?

Thanks for your help,
Sandra
sescandor is offline   Reply With Quote
Old 02-17-11, 09:02 AM   #2
sescandor
Registered User
 
Join Date: Feb 2011
Posts: 3
Talking Re: nVidia API for GPU temperature?

Also one more thing I thought about - a different way to pose the problem, I suppose: Is it possible to get the GPU info from the sysfs or something like that, since the nvidia X driver (nv-settings talks to it) has to be giving this info from somewhere, right?

I've been able to find the device entry in sysfs using lspci, now just what (if anything) can I do with this sort of information? I've seen something (briefly) on accessing pci memory, but I'm not sure if this the direction I should take if I'm looking for the GPU temperature (by not using libXNVCtrl).

Thanks for your help,
Sandra
sescandor is offline   Reply With Quote
Old 02-17-11, 10:50 AM   #3
mlord
Linux kernel hacker
 
Join Date: Feb 2009
Location: Ottawa, Canada
Posts: 173
Default Re: nVidia API for GPU temperature?

One way to avoid linking with unwanted libraries is to use the vendor provided binary to read the temperature. Eg, like this:
Code:
/* Free code sample by Mark Lord */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int nv_get_temperature (int *temperature)
{
        int err = EIO;
        char token[32];
        FILE *fp = popen("nvidia-smi -q -g 0", "r");

        if (fp == NULL) {
                err = errno;
                perror("nvidia-smi");
                return err;
        }
        while (1 == fscanf(fp, " %31s ", token)) {
                if (0 == strcmp(token, "Temperature")) {
                        if (1 == fscanf(fp, " : %4s C", token)) {
                                *temperature = atoi(token);
                                err = 0;  /* success */
                                break;
                        }
                }
        }
        fclose(fp);
        if (err)
                fprintf(stderr, "nvidia-smi did not report the temperature\n");
        return err;
}
                                                                                                
int main (void)                                                                                 
{                                                                                               
        int err, temperature;                                                                   
                                                                                                
        err = nv_get_temperature(&temperature);                                                 
        if (!err)                                                                               
                printf("Temperature: %d C\n", temperature);                                     
        return err;                                                                             
}

Quote:
I also have a constraint that I can't fork another process that runs the command for nvidia-settings, and then pipe the data from the output of nvidia-settings to the process that is running my code.
That's a strange constraint. The code above does something along those lines.
mlord is offline   Reply With Quote
Old 02-17-11, 11:05 AM   #4
mlord
Linux kernel hacker
 
Join Date: Feb 2009
Location: Ottawa, Canada
Posts: 173
Default Re: nVidia API for GPU temperature?

If you really want to artificially constrain things and live very dangerously, then just "strace nvdia-smi" and then mimic the exact same ioctl() calls that it does, using the exact same parameters. No libraries or forks required, and the code will break the next time NVIDIA updates the ioctl() interface they use.

Cheers
mlord is offline   Reply With Quote
Old 02-17-11, 01:33 PM   #5
sescandor
Registered User
 
Join Date: Feb 2011
Posts: 3
Default Re: nVidia API for GPU temperature?

Thank you for your reply, mlord! This strace (I'm sure you can tell I'm a Linux n00b ) will be quite useful for me for another project!

I got a hold of nVidia support and was told that libXNVCtrl is actually under MIT license! Initially, I had thought that it was under GPL, since the package descriptions for it (i.e Fedora) said that it was GPL (and plus the GPL notice in nvidia-settings source code).

So, for anyone who has been pulling his/her hair out for days trying to figure this out - here it is.

(Thanks to Mike from nVidia Support...wherever you are.)
sescandor is offline   Reply With Quote
Old 02-17-11, 02:44 PM   #6
AaronP
NVIDIA Corporation
 
AaronP's Avatar
 
Join Date: Mar 2005
Posts: 2,487
Default Re: nVidia API for GPU temperature?

Yeah, sorry about that. libXNVCtrl and nvidia-settings sources live in the same package but are under different licenses, confusing everybody. I've been meaning to split libXNVCtrl out into its own package, but so far just pointing out that it's X11-licensed has been easier.
AaronP is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:16 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.