GVM User Suite
User tools for the GVM open source project.
device.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 2666680 Ontario Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  */
19 #include <iostream>
20 
21 #include <unistd.h>
22 
23 #include <gpu/nvidia/device.h>
24 
25 #include <utils/colors.h>
26 #include <utils/device.h>
27 
28 using std::cout;
29 
105 bool dev_check()
106 {
107  return device_exists("/dev/tty") && device_exists("tty");
108 }
109 
111 {
112  return get_major("invalid-gvm") == -1;
113 }
114 
116 {
117  return get_major("mem") == 1;
118 }
119 
120 bool nvmode()
121 {
122  return get_param("/proc/driver/nvidia/params", "DeviceFileMode") == 438;
123 }
124 
125 bool nvctl()
126 {
127  int fd = nv_open_dev(255);
128  bool ret = false;
129 
130  if (fd != -1) {
131  ret = true;
132  close(fd);
133  }
134 
135  return ret;
136 }
137 
138 bool nvdev()
139 {
140  int fd = nv_open_dev(0);
141  bool ret = false;
142 
143  if (fd != -1) {
144  ret = true;
145  close(fd);
146  }
147 
148  return ret;
149 }
150 
151 int main()
152 {
153  const uint32_t NUM_TESTS = 6;
154 
155  const std::string test_names[] = {
156  "Device Check Test",
157  "Device Major Number Identifier Null",
158  "Device Major Number Identifier",
159  "DeviceFileMode Nvidia Check",
160  "Open /dev/nvidiactl File",
161  "Open /dev/nvidia0 File"
162  };
163  const std::string test_details[] = {
164  "The device \"tty\" and \"/dev/tty\" do not exist on the system.",
165  "The device major \"invalid-gvm\" is not equal to -1.",
166  "The device major \"mem\" is not equal to 1.",
167  "The Nvidia Device File Mode is not equal to 438.",
168  "Could not open nvidiactl file, try using sudo to run this test.",
169  "Could not open nvidia0 file, try using sudo to run this test."
170  };
171 
172  bool (*tests[])(void) = {
174  nvctl, nvdev
175  };
176 
177  uint32_t failures = 0;
178 
179  for (uint32_t i = 0; i < NUM_TESTS; ++i) {
180  bool test_result = tests[i]() == 1;
181 
182  if (test_result)
183  cout << GREEN_COLOR << test_names[i]
184  << " succeeded" RESET_COLOR "\n";
185  else {
186  ++failures;
187  cout << RED_COLOR << test_names[i]
188  << ": " << test_details[i]
189  << RESET_COLOR "\n";
190  }
191  }
192 
193  if (failures == 0)
194  cout << GREEN_COLOR << "All tests succeeded" RESET_COLOR "\n";
195  else
196  cout << RED_COLOR << failures << "/"
197  << NUM_TESTS << " tests failed" RESET_COLOR "\n";
198 }
#define RED_COLOR
Red color.
Definition: colors.h:30
#define GREEN_COLOR
Green color.
Definition: colors.h:33
#define RESET_COLOR
Reset color.
Definition: colors.h:51
bool nvctl()
Definition: device.cpp:125
bool nvmode()
Definition: device.cpp:120
bool nvdev()
Definition: device.cpp:138
bool dev_major_ok()
Definition: device.cpp:115
bool dev_check()
Definition: device.cpp:105
bool dev_major_null()
Definition: device.cpp:110
int main()
Definition: device.cpp:151
int nv_open_dev(uint16_t minor)
Opens a traditional NVIDIA device.
Definition: device.c:65
uint8_t device_exists(const char *device_name)
Does a device file exist?
Definition: device.c:30
int32_t get_major(const char *device)
Gets the device major id from /proc/devices.
Definition: device.c:42
int32_t get_param(const char *filename, const char *parameter)
Gets a parameter from a driver param file.
Definition: device.c:71