2020-03-04 15:51:16 -05:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2020 - present Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include "rdc_lib/rdc_common.h"
|
|
|
|
|
#include "rdc/rdc.h"
|
2020-03-10 14:02:05 -04:00
|
|
|
#include "rdc_lib/RdcException.h"
|
2020-03-04 15:51:16 -05:00
|
|
|
#include "RdciDiscoverySubSystem.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace amd {
|
|
|
|
|
namespace rdc {
|
|
|
|
|
|
2020-03-10 14:02:05 -04:00
|
|
|
RdciDiscoverySubSystem::RdciDiscoverySubSystem() : show_help_(false) {
|
2020-03-04 15:51:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RdciDiscoverySubSystem::parse_cmd_opts(int argc, char ** argv) {
|
|
|
|
|
const int HOST_OPTIONS = 1000;
|
2020-07-14 15:20:08 -04:00
|
|
|
const int JSON_OPTIONS = 1001;
|
2020-03-04 15:51:16 -05:00
|
|
|
const struct option long_options[] = {
|
|
|
|
|
{"host", required_argument, nullptr, HOST_OPTIONS },
|
|
|
|
|
{"help", optional_argument, nullptr, 'h' },
|
2020-03-10 14:02:05 -04:00
|
|
|
{"unauth", optional_argument, nullptr, 'u' },
|
2020-03-18 08:58:48 -04:00
|
|
|
{"list", optional_argument, nullptr, 'l' },
|
2020-07-14 15:20:08 -04:00
|
|
|
{"json", optional_argument, nullptr, JSON_OPTIONS },
|
2020-03-04 15:51:16 -05:00
|
|
|
{ nullptr, 0 , nullptr, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int option_index = 0;
|
|
|
|
|
int opt = 0;
|
2020-03-18 08:58:48 -04:00
|
|
|
bool is_list = false;
|
2020-03-04 15:51:16 -05:00
|
|
|
|
2020-03-18 08:58:48 -04:00
|
|
|
while ((opt = getopt_long(argc, argv, "hlu",
|
2020-03-04 15:51:16 -05:00
|
|
|
long_options, &option_index)) != -1) {
|
|
|
|
|
switch (opt) {
|
|
|
|
|
case HOST_OPTIONS:
|
|
|
|
|
ip_port_ = optarg;
|
|
|
|
|
break;
|
2020-07-14 15:20:08 -04:00
|
|
|
case JSON_OPTIONS:
|
|
|
|
|
set_json_output(true);
|
|
|
|
|
break;
|
2020-03-04 15:51:16 -05:00
|
|
|
case 'h':
|
|
|
|
|
show_help_ = true;
|
|
|
|
|
return;
|
2020-03-10 14:02:05 -04:00
|
|
|
case 'u':
|
|
|
|
|
use_auth_ = false;
|
|
|
|
|
break;
|
2020-03-18 08:58:48 -04:00
|
|
|
case 'l':
|
|
|
|
|
is_list = true;
|
|
|
|
|
break;
|
2020-03-04 15:51:16 -05:00
|
|
|
default:
|
|
|
|
|
show_help();
|
|
|
|
|
throw RdcException(RDC_ST_BAD_PARAMETER,
|
|
|
|
|
"Unknown command line options");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-10 14:02:05 -04:00
|
|
|
|
2020-03-18 08:58:48 -04:00
|
|
|
if (!is_list) {
|
|
|
|
|
show_help();
|
|
|
|
|
throw RdcException(RDC_ST_BAD_PARAMETER,
|
|
|
|
|
"Need to specify operations");
|
|
|
|
|
}
|
2020-03-04 15:51:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RdciDiscoverySubSystem::show_help() const {
|
2020-07-14 15:20:08 -04:00
|
|
|
if (is_json_output()) return;
|
2020-03-04 15:51:16 -05:00
|
|
|
std::cout << " discovery -- Used to discover and identify GPUs "
|
|
|
|
|
<< "and their attributes.\n\n";
|
|
|
|
|
std::cout << "Usage\n";
|
2020-07-14 15:20:08 -04:00
|
|
|
std::cout << " rdci discovery [--host <IP/FQDN>:port] [--json]"
|
|
|
|
|
<< " [-u] -l\n";
|
2020-03-04 15:51:16 -05:00
|
|
|
std::cout << "\nFlags:\n";
|
2020-03-10 14:02:05 -04:00
|
|
|
show_common_usage();
|
2020-07-14 15:20:08 -04:00
|
|
|
std::cout << " --json "
|
|
|
|
|
<< "Output using json.\n";
|
2020-03-18 08:58:48 -04:00
|
|
|
std::cout << " -l --list list GPU discovered"
|
|
|
|
|
<<" on the system\n";
|
2020-03-04 15:51:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RdciDiscoverySubSystem::process() {
|
|
|
|
|
if (show_help_) {
|
|
|
|
|
return show_help();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t gpu_index_list[RDC_MAX_NUM_DEVICES];
|
|
|
|
|
uint32_t count = 0;
|
2020-03-18 08:58:48 -04:00
|
|
|
rdc_status_t result = rdc_device_get_all(rdc_handle_,
|
2020-03-04 15:51:16 -05:00
|
|
|
gpu_index_list, &count);
|
|
|
|
|
if (result != RDC_ST_OK) {
|
|
|
|
|
throw RdcException(result, "Fail to get device information");
|
|
|
|
|
}
|
|
|
|
|
if (count == 0) {
|
2020-07-14 15:20:08 -04:00
|
|
|
if (is_json_output()) {
|
|
|
|
|
std::cout << "\"gpus\" : [], \"status\": \"ok\"";
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "No GPUs find on the system\n";
|
|
|
|
|
}
|
2020-03-04 15:51:16 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 15:20:08 -04:00
|
|
|
if (is_json_output()) {
|
|
|
|
|
std::cout << "\"gpus\" : [";
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << count << " GPUs found.\n";
|
|
|
|
|
std::cout << "------------------------------------------------"
|
|
|
|
|
<< "-----------------\n";
|
|
|
|
|
std::cout << "GPU Index\t Device Information\n";
|
|
|
|
|
}
|
2020-03-04 15:51:16 -05:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
|
rdc_device_attributes_t attribute;
|
2020-03-18 08:58:48 -04:00
|
|
|
result = rdc_device_get_attributes(rdc_handle_,
|
2020-03-04 15:51:16 -05:00
|
|
|
gpu_index_list[i], &attribute);
|
|
|
|
|
if (result != RDC_ST_OK) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-14 15:20:08 -04:00
|
|
|
if (is_json_output()) {
|
|
|
|
|
std::cout << "{\"gpu_index\": \"" << i << "\", \"device_name\": \""
|
|
|
|
|
<< attribute.device_name << "\"}";
|
|
|
|
|
if (i != count -1) {
|
|
|
|
|
std::cout << ",";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << i << "\t\t" << attribute.device_name <<std::endl;
|
|
|
|
|
}
|
2020-03-04 15:51:16 -05:00
|
|
|
}
|
2020-07-14 15:20:08 -04:00
|
|
|
if (is_json_output()) {
|
|
|
|
|
std::cout << ']';
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "------------------------------------------------"
|
2020-03-04 15:51:16 -05:00
|
|
|
<< "-----------------\n";
|
2020-07-14 15:20:08 -04:00
|
|
|
}
|
2020-03-04 15:51:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace rdc
|
|
|
|
|
} // namespace amd
|
|
|
|
|
|
|
|
|
|
|