Added support for XNVCTRL on Linux to add support for --gpu-temp-retain for NVidia GPU
[hashcat.git] / src / ext_xnvctrl.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * License.....: MIT
4 */
5
6 #include <ext_xnvctrl.h>
7
8 int xnvctrl_init (XNVCTRL_PTR *xnvctrl)
9 {
10 if (!xnvctrl) return (-1);
11
12 memset (xnvctrl, 0, sizeof (XNVCTRL_PTR));
13
14 #ifdef _WIN
15
16 // unsupport platform?
17 return (-1);
18
19 #elif _POSIX
20
21 xnvctrl->lib_x11 = dlopen ("libX11.so", RTLD_LAZY);
22
23 if (xnvctrl->lib_x11 == NULL)
24 {
25 if (data.quiet == 0) log_info ("WARNING: load X11 library failed, proceed without X11 HWMon enabled.");
26
27 return -1;
28 }
29
30 xnvctrl->lib_xnvctrl = dlopen ("libXNVCtrl.so", RTLD_LAZY);
31
32 if (xnvctrl->lib_xnvctrl == NULL)
33 {
34 xnvctrl->lib_xnvctrl = dlopen ("libXNVCtrl.so.0", RTLD_LAZY);
35
36 if (xnvctrl->lib_xnvctrl == NULL)
37 {
38 if (data.quiet == 0) log_info ("WARNING: load XNVCTRL library failed, proceed without XNVCTRL HWMon enabled.");
39
40 return -1;
41 }
42 }
43
44 xnvctrl->XOpenDisplay = dlsym (xnvctrl->lib_x11, "XOpenDisplay");
45 xnvctrl->XCloseDisplay = dlsym (xnvctrl->lib_x11, "XCloseDisplay");
46
47 xnvctrl->XNVCTRLQueryTargetAttribute = dlsym (xnvctrl->lib_xnvctrl, "XNVCTRLQueryTargetAttribute");
48 xnvctrl->XNVCTRLSetTargetAttribute = dlsym (xnvctrl->lib_xnvctrl, "XNVCTRLSetTargetAttribute");
49
50 #endif
51
52 // not using HC_LOAD_FUNC() here, because we're using 2 libraries and therefore have 2 different variable names for them
53
54 return 0;
55 }
56
57 void xnvctrl_close (XNVCTRL_PTR *xnvctrl)
58 {
59 if (xnvctrl)
60 {
61 #if _POSIX
62
63 if (xnvctrl->lib_x11)
64 {
65 dlclose (xnvctrl->lib_x11);
66 }
67
68 if (xnvctrl->lib_xnvctrl)
69 {
70 dlclose (xnvctrl->lib_xnvctrl);
71 }
72
73 #endif
74
75 myfree (xnvctrl);
76 }
77 }
78
79 int hm_XNVCTRL_XOpenDisplay (XNVCTRL_PTR *xnvctrl)
80 {
81 void *dpy = xnvctrl->XOpenDisplay (NULL);
82
83 if (dpy == NULL)
84 {
85 return -1;
86 }
87
88 xnvctrl->dpy = dpy;
89
90 return 0;
91 }
92
93 void hm_XNVCTRL_XCloseDisplay (XNVCTRL_PTR *xnvctrl)
94 {
95 xnvctrl->XCloseDisplay (xnvctrl->dpy);
96 }
97
98 int get_fan_control (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
99 {
100 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_COOLER_MANUAL_CONTROL, val);
101
102 if (!rc) return -1;
103
104 return 0;
105 }
106
107 int set_fan_control (XNVCTRL_PTR *xnvctrl, int gpu, int val)
108 {
109 xnvctrl->XNVCTRLSetTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_COOLER_MANUAL_CONTROL, val);
110
111 int cur;
112
113 int rc = get_fan_control (xnvctrl, gpu, &cur);
114
115 if (rc == -1) return -1;
116
117 if (cur != val) return -1;
118
119 return 0;
120 }
121
122 int get_core_threshold (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
123 {
124 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_GPU, gpu, 0, NV_CTRL_GPU_CORE_THRESHOLD, val);
125
126 if (!rc) return -1;
127
128 return 0;
129 }
130
131 int get_fan_speed_current (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
132 {
133 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_CURRENT_LEVEL, val);
134
135 if (!rc) return -1;
136
137 return 0;
138 }
139
140 int get_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int *val)
141 {
142 int rc = xnvctrl->XNVCTRLQueryTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_LEVEL, val);
143
144 if (!rc) return -1;
145
146 return 0;
147 }
148
149 int set_fan_speed_target (XNVCTRL_PTR *xnvctrl, int gpu, int val)
150 {
151 xnvctrl->XNVCTRLSetTargetAttribute (xnvctrl->dpy, NV_CTRL_TARGET_TYPE_COOLER, gpu, 0, NV_CTRL_THERMAL_COOLER_LEVEL, val);
152
153 int cur;
154
155 int rc = get_fan_speed_target (xnvctrl, gpu, &cur);
156
157 if (rc == -1) return -1;
158
159 if (cur != val) return -1;
160
161 return 0;
162 }