Coder Social home page Coder Social logo

Comments (2)

wookayin avatar wookayin commented on June 11, 2024

We are using nvmlDeviceGetFanSpeed, but another API nvmlUnitGetFanSpeedInfo seems to allow reading of RPM values.

from gpustat.

ricciolino avatar ricciolino commented on June 11, 2024

I already tried it but it does not seem to work in my case (RTX 3080). I got an "Invalid argument" exception. I guess it is only intended to be used with a specific class of GPU models (S-class unit).

image

So finally I was able to compile a simple snippet of C code to get what I wanted.
I exploit the NVCtrl library.
Check this thread (me author) on ubuntuforums.
Here is the snippet:

/*

Compile:
gcc -o get_GPU_fan_RPM_values get_GPU_fan_RPM_values.c -lX11 -lXNVCtrl

Usage:
./get_GPU_fan_RPM_values <fan_id>

*/


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <NVCtrl/NVCtrl.h>
#include <NVCtrl/NVCtrlLib.h>

static Display *display;

static double get_att(int target, int id, int att)
{
	Bool res;
	int temp;

	res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);

	if (res == True)
		return temp;

	return -1;
}

void nvidia_cleanup(void)
{
	if (display) {
		XCloseDisplay(display);
		display = NULL;
	}
}

bool isFanId(char number[])
{
    int i = 0;
    if (number[0] == '-')
        return false;
    for (; number[i] != 0; i++)
    {
        if (number[i] > '1' || number[i] < '0')
            return false;
    }
    return true;
}

int main(int argc, char** argv) {

    if ( !(argc == 2) ) {
		printf("Please, provide the fan ID.\n");
		return 1;
    }
    if ( !isFanId(argv[1]) ) {
		printf("There is no fan with ID %d.\n", atoi(argv[1]));
		return 1;
    }

    int fanId = atoi(argv[1]);

	display = XOpenDisplay(NULL);

    if (!display) {
		printf("Cannot open connection to X11 server.\n");
        nvidia_cleanup();
		return 1;
	}

	int evt, err;
	if (!XNVCTRLQueryExtension(display, &evt, &err)) {
		printf("XNVCTRLQueryExtension error %d (evt %d).\n", err, evt);
        nvidia_cleanup();
		return 1;
    }

    printf("%d\n", (int)get_att(NV_CTRL_TARGET_TYPE_COOLER, fanId, NV_CTRL_THERMAL_COOLER_SPEED));

    nvidia_cleanup();
    
    return 0;
}

from gpustat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.