34 #if defined(__linux__) || defined(__APPLE__)
54 #include <cutils/properties.h>
56 #define LOG_TAG "ni_rsrc_mon_logan"
63 static BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
89 #elif defined(__linux__)
90 void setup_signal_handler(
void);
100 void sig_handler(
int sig)
102 if (sig == SIGTERM || sig == SIGINT)
106 else if (sig == SIGHUP)
108 printf(
"SIGHUP, reloading p_config ? ");
109 setup_signal_handler();
120 void setup_signal_handler(
void)
122 if (signal(SIGTERM, sig_handler) == SIG_ERR ||
123 signal(SIGHUP, sig_handler) == SIG_ERR ||
124 signal(SIGINT, sig_handler) == SIG_ERR)
126 fprintf(stderr,
"Error %d: signal handler setup\n",
NI_ERRNO);
137 int get_numa_node(
const char *device_name)
143 char cmd_ret[64] = {0};
149 ptr = (
char *)(device_name + 5);
150 snprintf(cmd,
sizeof(cmd) - 1,
"cat /sys/block/%s/device/*/numa_node",ptr);
151 cmd_fp = popen(cmd,
"r");
156 if (fgets(cmd_ret,
sizeof(cmd_ret)/
sizeof(cmd_ret[0]), cmd_fp) == NULL)
186 const size_t len = strlen(numArray);
188 for (i = 0; i < len; i++)
190 if (!isdigit(numArray[i]))
192 fprintf(stderr,
"invalid, ABORTING\n");
197 return len == i ?
atoi(numArray) : 0;
210 if ( *(int32_t*)a < *(int32_t*)b )
return -1;
211 if ( *(int32_t*)a == *(int32_t*)b )
return 0;
212 if ( *(int32_t*)a > *(int32_t*)b )
return 1;
231 int module_count = 0;
233 int *module_id_arr = NULL;
234 int best_load_module_id = -1;
240 snprintf(module_name,
sizeof(module_name),
"decoder");
241 module_id_arr = (
int *)malloc(
sizeof(
int) * module_count);
244 fprintf(stderr,
"ERROR %d: Failed to allocate memory for ni_logan_rsrc_mon print_perf()",
248 memcpy(module_id_arr, coders->
decoders,
sizeof(
int) * module_count);
253 snprintf(module_name,
sizeof(module_name),
"encoder");
254 module_id_arr = (
int *)malloc(
sizeof(
int) * module_count);
257 fprintf(stderr,
"ERROR %d: Failed to allocate memory for ni_logan_rsrc_mon print_perf()",
261 memcpy(module_id_arr, coders->
encoders,
sizeof(
int) * module_count);
265 fprintf(stderr,
"ERROR: Unknown device type: %d\n", module_type);
272 fprintf(stderr,
"ERROR %d: Failed to allocate memory for ni_logan_rsrc_mon print_perf()",
278 memcpy((
void*)&best_load_module_id, module_id_arr,
sizeof(
int));
281 if (outformattype == 0)
283 printf(
"Num %ss: %d\n", module_name, module_count);
290 if (outformattype == 0)
292 printf(
"%-4s %-5s %-4s %-10s %-4s %-4s %-14s %-20s\n",
"BEST",
"INDEX",
293 "LOAD",
"MODEL_LOAD",
"MEM",
"INST",
"DEVICE",
"NAMESPACE");
295 else if (outformattype == 2 || outformattype == 3)
299 printf(
"{\n \"decoders\": [\n");
303 printf(
" \"encoders\": [\n");
307 for (i = 0; i < module_count; i++)
312 if (p_device_context)
321 fprintf(stderr,
"Error open device %s, blk device %s\n",
330 if (NI_INVALID_EVENT_HANDLE == sessionCtxt->
event_handle)
334 fprintf(stderr,
"ERROR %d: print_perf() create envet\n",
NI_ERRNO);
347 fprintf(stderr,
"Error query %s %s %s.%d\n", module_name,
365 char best_load_print[2] =
" ";
368 strncpy(best_load_print,
"L", 1);
372 if(outformattype == 0)
374 printf(
"%s%s%s %-5d %-4d %-10d %-4d %-4d %-14s %-20s\n", best_load_print,
" ",
" ",
383 else if (outformattype == 1)
389 " \"%s\" : \"%s\",\n"
395 " \"%s\" : \"%s\",\n"
396 " \"%s\" : \"%s\",\n"
403 module_name,
"NUMBER", module_count,
"BEST", best_load_print,
416 else if (outformattype == 3)
420 " \"%s\" : \"%s\",\n"
426 " \"%s\" : \"%s\",\n"
427 " \"%s\" : \"%s\",\n"
434 "NUMBER", module_count,
"BEST", best_load_print,
448 if (i < module_count - 1)
461 " \"%s\" : \"%s\",\n"
467 " \"%s\" : \"%s\",\n"
468 " \"%s\" : \"%s\",\n"
473 "NUMBER", module_count,
"BEST", best_load_print,
485 if (i < module_count - 1)
498 if (outformattype == 2 || outformattype == 3)
522 int main(
int argc,
char *argv[])
526 int should_match_rev = 1;
527 int init_no_compat_check = 0;
528 int init_only_if_full_compat = 0;
541 time_t startTime = { 0 }, now = { 0 };
542 int timeout_seconds = 0;
543 struct tm *ltime = NULL;
544 char buf[64] = { 0 };
545 long long time_diff_hours, time_diff_minutes, time_diff_seconds;
552 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
553 #elif defined(__linux__)
554 setup_signal_handler();
558 while ((opt =
getopt(argc, argv,
"hvrcn:o:t:l:i")) != -1)
564 if (!strcmp(
optarg,
"json"))
568 else if (!strcmp(
optarg,
"text"))
572 else if (!strcmp(
optarg,
"json1"))
576 else if (!strcmp(
optarg,
"json2"))
582 fprintf(stderr,
"Error: unknown selection for outputFormat: %s\n",
optarg);
591 init_no_compat_check = 1;
594 init_only_if_full_compat = 1;
598 printf(
"Timeout will be set %d\n", timeout_seconds);
601 if (!strcmp(
optarg,
"none")) {
603 }
else if (!strcmp(
optarg,
"fatal")) {
605 }
else if (!strcmp(
optarg,
"error")) {
607 }
else if (!strcmp(
optarg,
"info")) {
609 }
else if (!strcmp(
optarg,
"debug")) {
611 }
else if (!strcmp(
optarg,
"trace")) {
614 fprintf(stderr,
"unknown log level selected: %s",
optarg);
627 printf(
"-------- ni_rsrc_mon_logan v%s --------\n"
628 "The ni_rsrc_mon_logan program provides a real-time view of NETINT Logan T4XX \n"
629 "resources running on the system.\n"
630 "return 0 on success\n"
631 "return 1 on failure\n"
632 "Usage: sudo ni_rsrc_mon_logan [OPTIONS]\n"
633 "-o Output print format: text|json||json2\n"
634 "-n Specify reporting interval in one second interval.\n"
635 " If 0 or no selection, report only once.\n Default: 0\n"
636 "-r Init transcoder card resource regardless firmware release \n"
637 " version to libxcoder_logan version compatibility.\n"
638 " Default: only init cards with compatible firmware version.\n"
639 "-c Only init if all cards are fully compatible. Default is to init as many \n"
640 " fully and partially cards as possible."
641 "-t Set timeout time in seconds for device polling. Program will \n"
642 " exit with failure if timeout is reached without finding at \n"
643 " least one device. If 0 or no selection, poll indefinitely \n"
644 " until a T4XX device is found.\n"
646 "-l Set loglevel of libxcoder_logan API.\n"
647 " [none, fatal, error, info, debug, trace]\n"
649 "-v Show libxcoder_logan version.\n"
650 "-i Do not refresh the devices list.\n"
651 "-h Open this help message.\n"
653 "Reporting columns\n"
654 "BEST flag showing card of lowest realtime load and to be selected for \n"
655 " next auto allocated job\n"
656 "INDEX index number used by resource manager to identify the resource\n"
657 "LOAD realtime load\n"
658 "MODEL_LOAD estimated load based on framerate and resolution\n"
659 "INST number of job instances\n"
660 "DEVICE path to NVMe device file handle\n"
665 fprintf(stderr,
"Option -o, -n, or -l require an argument, use option -h for help, "
666 "all other options are not supported.\n");
668 #elif defined(__linux__)
671 fprintf(stderr,
"Option -%c requires an argument.\n",
optopt);
676 fprintf(stderr,
"Unknown option `-%c'.\n",
optopt);
681 fprintf(stderr,
"Unknown option character `\\x%x'.\n",
optopt);
687 fprintf(stderr,
"ABORTING\n");
692 if (init_no_compat_check && init_only_if_full_compat)
694 fprintf(stderr,
"Error: -r option cannot be used with -c option\n");
697 if (init_no_compat_check)
699 should_match_rev = 0;
701 else if (init_only_if_full_compat)
703 should_match_rev = 2;
706 if ((argc <= 2) && (
optind == 1))
720 fprintf(stderr,
"Error access NI resource, quit ..\n");
727 fprintf(stderr,
"Error get Coders info ..\n");
735 printf(
"**************************************************\n");
738 startTime = time(NULL);
740 system(
"chown mediacodec:mediacodec /dev/shm_netint/*");
745 ltime = localtime(&now);
748 strftime(buf,
sizeof(buf),
"%c", ltime);
750 time_diff_seconds = (
long long)difftime(now, startTime);
751 time_diff_minutes = time_diff_seconds / 60;
752 time_diff_hours = time_diff_minutes / 60;
758 printf(
"Error: resource pool records might be corrupted!!\n");
765 printf(
"%s up %02lld" ":%02lld" ":%02lld" " v%s\n", buf, time_diff_hours, time_diff_minutes % 60, time_diff_seconds % 60,
772 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
774 fprintf(stderr,
"ERROR: Failed to obtain mutex: %p\n", p_device_pool->
lock);
777 #elif defined(__linux__)
778 if ( flock(p_device_pool->
lock, LOCK_EX) )
780 fprintf(stderr,
"Error flock() failed\n");
787 ReleaseMutex((HANDLE)p_device_pool->
lock);
788 #elif defined(__linux__)
789 if ( flock(p_device_pool->
lock, LOCK_UN) )
791 fprintf(stderr,
"Error flock() failed\n");
800 printf(
"**************************************************\n");
805 if (checkInterval == 0)
815 system(
"chown mediacodec:mediacodec /dev/shm_netint/*");
816 system(
"chmod 777 /dev/block/nvme*");
Common NETINT definitions used by all modules.
@ NI_LOGAN_RETCODE_SUCCESS
#define NI_LOGAN_XCODER_REVISION
@ NI_LOGAN_DEVICE_TYPE_ENCODER
@ NI_LOGAN_DEVICE_TYPE_DECODER
ni_device_handle_t ni_logan_device_open(const char *p_dev, uint32_t *p_max_io_size_out)
Opens device and returnes device device_handle if successful.
ni_logan_retcode_t ni_logan_device_session_query(ni_logan_session_context_t *p_ctx, ni_logan_device_type_t device_type)
Query session data from the device - Currently not implemented If device_type is NI_LOGAN_DEVICE_TYPE...
void ni_logan_device_session_context_init(ni_logan_session_context_t *p_ctx)
Initialize already allocated session context to a known state.
void ni_logan_close_event(ni_event_handle_t event_handle)
Closes event and releases resources.
void ni_logan_device_session_context_clear(ni_logan_session_context_t *p_ctx)
Clear already allocated session context to all zeros.
ni_event_handle_t ni_logan_create_event(void)
Create event and returnes event handle if successful.
void ni_logan_device_close(ni_device_handle_t device_handle)
Closes device and releases resources.
Main NETINT device API header file provides the ability to communicate with NI T-408 type hardware tr...
int getopt(int argc, char *argv[], const char *optstring)
void ni_log_set_level(ni_log_level_t level)
Set ni_log_level.
ni_logan_device_context_t * ni_logan_rsrc_get_device_context(ni_logan_device_type_t device_type, int guid)
Allocates and returns a pointer to ni_logan_device_context_t struct based on provided device_type and...
void ni_logan_rsrc_free_device_pool(ni_logan_device_pool_t *p_device_pool)
Free all resources taken by the device pool.
void ni_logan_rsrc_free_device_context(ni_logan_device_context_t *p_device_context)
Free previously allocated device context.
int ni_logan_rsrc_init(int should_match_rev, int timeout_seconds)
Initialize and create all resources required to work with NETINT NVMe transcoder devices....
ni_logan_retcode_t ni_logan_rsrc_refresh(int should_match_rev)
Scan and refresh all resources on the host, taking into account hot-plugged and pulled out cards.
ni_logan_device_pool_t * ni_logan_rsrc_get_device_pool(void)
Create and return the allocated ni_logan_device_pool_t struct.
Exported definitions related to resource management of NI T-408 devices.
int main(int argc, char *argv[])
int compareInt32_t(const void *a, const void *b)
compare two int32_t for qsort
int argToI(char *numArray)
convert number from argv input to integer if safe
void print_perf(ni_logan_device_type_t module_type, ni_logan_device_queue_t *coders, ni_logan_session_context_t *sessionCtxt, int outformattype)
print performance data for either decoder or encoder
uint32_t g_logan_xcoder_stop_process
Private definitions related to resource management of NI T-408 devices.
void ni_logan_usleep(int64_t usec)
Exported utility routines definition.
ni_logan_device_info_t * p_device_info
char dev_name[NI_LOGAN_MAX_DEVICE_NAME_LEN]
char blk_name[NI_LOGAN_MAX_DEVICE_NAME_LEN]
ni_logan_device_queue_t * p_device_queue
int32_t decoders[LOGAN_MAX_DEVICE_CNT]
int32_t encoders[LOGAN_MAX_DEVICE_CNT]
uint32_t fw_video_mem_usage
ni_logan_load_query_t load_query
ni_device_handle_t device_handle
ni_event_handle_t event_handle
ni_device_handle_t blk_io_handle
uint32_t max_nvme_io_size
uint8_t composite_temperature