Initial commit
[hashcat.git] / src / ext_smi.c
1 /**
2 * Author......: Jens Steube <jens.steube@gmail.com>
3 * License.....: MIT
4 */
5
6 #include <ext_smi.h>
7
8 int hc_nvidia_smi (int dev, int *temperature, int *gpu)
9 {
10 char cmd[256]; memset (cmd, 0, sizeof (cmd));
11
12 sprintf (cmd, "nvidia-smi -q -g %d", dev);
13
14 FILE *fp = popen (cmd, "r");
15
16 if (fp == NULL)
17 {
18 log_info ("WARN: %s\n", "nvidia-smi is missing!");
19
20 return SMI_NOBIN;
21 }
22
23 int temp_found = 0;
24 int util_found = 0;
25
26 char token[32];
27
28 while (fscanf (fp, " %31s ", token) == 1)
29 {
30 if (strcmp (token, "Temperature") == 0)
31 {
32 if (fscanf (fp, " : %4s C", token) == 1) *temperature = atoi (token);
33
34 temp_found = 1;
35 }
36
37 if (temp_found == 1)
38 {
39 if ((strcmp (token, "GPU") == 0) || (strcmp (token, "Gpu") == 0))
40 {
41 if (fscanf (fp, " : %4s C", token) == 1) *temperature = atoi (token);
42
43 temp_found = 0;
44 }
45 }
46
47 if (strcmp (token, "Utilization") == 0)
48 {
49 util_found = 1;
50
51 temp_found = 0;
52 }
53
54 if (util_found == 1)
55 {
56 if ((strcmp (token, "GPU") == 0) || (strcmp (token, "Gpu") == 0))
57 {
58 if (fscanf (fp, " : %2s%%", token) == 1) *gpu = atoi (token);
59
60 util_found = 0;
61 }
62 }
63 }
64
65 pclose (fp);
66
67 return (SMI_OK);
68 }