24
24
"Try reducing polling interval"
25
25
26
26
/* Count of /dev/acrn_hvlog_cur_xxx */
27
- static unsigned int dev_cnt ;
27
+ static int cur_cnt , last_cnt ;
28
28
static unsigned long interval = DEFAULT_POLL_INTERVAL ;
29
29
30
30
/* this is for log file */
@@ -83,9 +83,8 @@ struct hvlog_dev {
83
83
84
84
size_t write_log_file (struct hvlog_file * log , const char * buf , size_t len );
85
85
86
- static int get_dev_cnt (void )
86
+ static int get_dev_cnt (char * prefix )
87
87
{
88
- char prefix [32 ] = "acrn_hvlog_cur_" ; /* acrnlog dev prefix */
89
88
struct dirent * pdir ;
90
89
int cnt = 0 ;
91
90
char * ret ;
@@ -354,8 +353,8 @@ static void *cur_read_func(void *arg)
354
353
char warn_msg [LOG_MSG_SIZE ] = {0 };
355
354
356
355
while (1 ) {
357
- hvlog_dev_read_msg (cur , dev_cnt );
358
- msg = get_min_seq_msg (cur , dev_cnt );
356
+ hvlog_dev_read_msg (cur , cur_cnt );
357
+ msg = get_min_seq_msg (cur , cur_cnt );
359
358
if (!msg ) {
360
359
usleep (interval );
361
360
continue ;
@@ -502,27 +501,32 @@ int main(int argc, char *argv[])
502
501
return ret ;
503
502
}
504
503
505
- dev_cnt = get_dev_cnt ();
506
- if (dev_cnt == 0 ) {
504
+ cur_cnt = get_dev_cnt ("acrn_hvlog_cur_" );
505
+ last_cnt = get_dev_cnt ("acrn_hvlog_last_" );
506
+
507
+ if (cur_cnt == 0 ) {
507
508
printf ("Failed to find acrn hvlog devices, please check whether module acrn_hvlog is inserted\n" );
508
509
return -1 ;
509
- }
510
+ } else if (cur_cnt == -1 )
511
+ return -1 ;
510
512
511
- cur = calloc (dev_cnt , sizeof (struct hvlog_data ));
513
+ cur = calloc (cur_cnt , sizeof (struct hvlog_data ));
512
514
if (!cur ) {
513
515
printf ("Failed to allocate buf for cur log buf\n" );
514
516
return -1 ;
515
517
}
516
518
517
- last = calloc (dev_cnt , sizeof (struct hvlog_data ));
518
- if (!last ) {
519
- printf ("Failed to allocate buf for last log buf\n" );
520
- free (cur );
521
- return -1 ;
519
+ if (last_cnt ) {
520
+ last = calloc (cur_cnt , sizeof (struct hvlog_data ));
521
+ if (!last ) {
522
+ printf ("Failed to allocate buf for last log buf\n" );
523
+ free (cur );
524
+ return -1 ;
525
+ }
522
526
}
523
527
524
528
num_cur = 0 ;
525
- for (i = 0 ; i < dev_cnt ; i ++ ) {
529
+ for (i = 0 ; i < cur_cnt ; i ++ ) {
526
530
if (snprintf (name , sizeof (name ), "/dev/acrn_hvlog_cur_%d" , i ) >= sizeof (name )) {
527
531
printf ("ERROR: cur hvlog path is truncated\n" );
528
532
return -1 ;
@@ -536,17 +540,19 @@ int main(int argc, char *argv[])
536
540
}
537
541
538
542
num_last = 0 ;
539
- for (i = 0 ; i < dev_cnt ; i ++ ) {
540
- if (snprintf (name , sizeof (name ), "/dev/acrn_hvlog_last_%d" , i ) >= sizeof (name )) {
541
- printf ("ERROR: last hvlog path is truncated\n" );
542
- return -1 ;
543
+ if (last_cnt ) {
544
+ for (i = 0 ; i < cur_cnt ; i ++ ) {
545
+ if (snprintf (name , sizeof (name ), "/dev/acrn_hvlog_last_%d" , i ) >= sizeof (name )) {
546
+ printf ("ERROR: last hvlog path is truncated\n" );
547
+ return -1 ;
548
+ }
549
+ last [i ].dev = hvlog_open_dev (name );
550
+ if (!last [i ].dev )
551
+ perror (name );
552
+ else
553
+ num_last ++ ;
554
+ last [i ].msg = NULL ;
543
555
}
544
- last [i ].dev = hvlog_open_dev (name );
545
- if (!last [i ].dev )
546
- perror (name );
547
- else
548
- num_last ++ ;
549
- last [i ].msg = NULL ;
550
556
}
551
557
552
558
printf ("open cur:%d last:%d\n" , num_cur , num_last );
@@ -562,8 +568,8 @@ int main(int argc, char *argv[])
562
568
563
569
if (num_last ) {
564
570
while (1 ) {
565
- hvlog_dev_read_msg (last , dev_cnt );
566
- msg = get_min_seq_msg (last , dev_cnt );
571
+ hvlog_dev_read_msg (last , cur_cnt );
572
+ msg = get_min_seq_msg (last , cur_cnt );
567
573
if (!msg )
568
574
break ;
569
575
write_log_file (& last_log , msg -> raw , msg -> len );
@@ -573,13 +579,17 @@ int main(int argc, char *argv[])
573
579
if (cur_thread )
574
580
pthread_join (cur_thread , NULL );
575
581
576
- for (i = 0 ; i < dev_cnt ; i ++ ) {
582
+ for (i = 0 ; i < cur_cnt ; i ++ ) {
577
583
hvlog_close_dev (cur [i ].dev );
584
+ }
585
+
586
+ for (i = 0 ; i < last_cnt ; i ++ ) {
578
587
hvlog_close_dev (last [i ].dev );
579
588
}
580
589
581
590
free (cur );
582
- free (last );
591
+ if (last_cnt )
592
+ free (last );
583
593
584
594
return 0 ;
585
595
}
0 commit comments