-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathva_trace.c
6370 lines (5456 loc) · 271 KB
/
va_trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009-2011 Intel Corporation. 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, sub license, 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 (including the
* next paragraph) 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 NON-INFRINGEMENT.
* IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
*/
#define _GNU_SOURCE 1
#include "sysdeps.h"
#include "va.h"
#include "va_enc_h264.h"
#include "va_backend.h"
#include "va_internal.h"
#include "va_trace.h"
#include "va_enc_h264.h"
#include "va_enc_jpeg.h"
#include "va_enc_vp8.h"
#include "va_dec_jpeg.h"
#include "va_dec_vp8.h"
#include "va_dec_vp9.h"
#include "va_dec_hevc.h"
#include "va_str.h"
#include "va_vpp.h"
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "va_drmcommon.h"
#if defined(_WIN32)
#include "win32/va_win32.h"
#include "compat_win32.h"
#else
#include <dlfcn.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#endif
#if defined(__linux__)
#include <sys/syscall.h>
#elif defined(__DragonFly__) || defined(__FreeBSD__)
#include <pthread_np.h>
#elif defined(__NetBSD__)
#include <lwp.h>
#elif defined(__sun)
#include <thread.h>
#endif
/* bionic, glibc >= 2.30, musl >= 1.3 have gettid(), so add va_ prefix */
static pid_t va_gettid()
{
#if defined(_WIN32)
return GetCurrentThreadId();
#elif defined(__linux__)
return syscall(__NR_gettid);
#elif defined(__DragonFly__) || defined(__FreeBSD__)
return pthread_getthreadid_np();
#elif defined(__NetBSD__)
return _lwp_self();
#elif defined(__OpenBSD__)
return getthrid();
#elif defined(__sun)
return thr_self();
#else
#warning "Cannot get kernel thread identifier on this platform."
return (intptr_t)pthread_self();
#endif
}
/*
* Env. to debug some issue, e.g. the decode/encode issue in a video conference scenerio:
* .LIBVA_TRACE=log_file: general VA parameters saved into log_file
* .LIBVA_TRACE=FTRACE: trace general VA parameters into linux ftrace framework, use trace-cmd to capture and parse by tracetool in libva-utils
* .LIBVA_TRACE_BUFDATA: dump all VA data buffer into log_file
* when LIBVA_TRACE in FTRACE mode, all data are redirected to linux ftrace, finally parsed by tracetool
* .LIBVA_TRACE_CODEDBUF=coded_clip_file: save the coded clip into file coded_clip_file
* .LIBVA_TRACE_SURFACE=yuv_file: save surface YUV into file yuv_file. Use file name to determine
* decode/encode or jpeg surfaces
* .LIBVA_TRACE_SURFACE_GEOMETRY=WIDTHxHEIGHT+XOFF+YOFF: only save part of surface context into file
* due to storage bandwidth limitation
*/
/* global settings */
/* LIBVA_TRACE */
int va_trace_flag = 0;
#define MAX_TRACE_CTX_NUM 64
#define TRACE_CTX_ID_MASK (MAX_TRACE_CTX_NUM - 1)
#define MAX_TRACE_THREAD_NUM 64
#define MAX_TRACE_BUF_INFO_HASH_SIZE 1024 // 1<<10
#define MAX_TRACE_BUF_INFO_HASH_LEVEL 3
#define TRACE_BUF_INFO_HASH_ID_MASK (MAX_TRACE_BUF_INFO_HASH_SIZE - 1)
struct trace_buf_info {
int valid;
VABufferID buf_id;
VAContextID ctx_id;
};
struct trace_buf_manager {
struct trace_buf_info *pbuf_info[MAX_TRACE_BUF_INFO_HASH_LEVEL];
};
struct trace_log_file {
pid_t thread_id;
int used;
char *fn_log;
FILE *fp_log;
};
struct trace_log_files_manager {
struct trace_log_file log_file[MAX_TRACE_THREAD_NUM];
};
/* per context settings */
struct trace_context {
struct trace_log_file *plog_file;
struct trace_log_file *plog_file_list[MAX_TRACE_THREAD_NUM];
/* LIBVA_TRACE_CODEDBUF */
FILE *trace_fp_codedbuf; /* save the encode result into a file */
char *trace_codedbuf_fn; /* file name */
/* LIBVA_TRACE_SURFACE */
FILE *trace_fp_surface; /* save the surface YUV into a file */
char *trace_surface_fn; /* file name */
VAContextID trace_context; /* current context */
VASurfaceID trace_rendertarget; /* current render target */
VAProfile trace_profile; /* current profile for buffers */
VAEntrypoint trace_entrypoint; /* current entrypoint */
unsigned int trace_frame_no; /* current frame NO */
unsigned int trace_slice_no; /* current slice NO */
unsigned int trace_slice_size; /* current slice buffer size */
unsigned int trace_surface_width; /* surface dumping geometry */
unsigned int trace_surface_height;
unsigned int trace_surface_xoff;
unsigned int trace_surface_yoff;
unsigned int trace_frame_width; /* current frame width */
unsigned int trace_frame_height; /* current frame height */
unsigned int pts; /* IVF header information */
pid_t created_thd_id;
};
struct trace_config_info {
int valid;
VAConfigID config_id;
VAProfile trace_profile;
VAEntrypoint trace_entrypoint;
pid_t created_thd_id;
};
struct va_trace {
struct trace_context *ptra_ctx[MAX_TRACE_CTX_NUM + 1];
int context_num;
struct trace_buf_manager buf_manager;
struct trace_log_files_manager log_files_manager;
struct trace_config_info config_info[MAX_TRACE_CTX_NUM];
char *fn_log_env;
char *fn_codedbuf_env;
char *fn_surface_env;
pthread_mutex_t resource_mutex;
pthread_mutex_t context_mutex;
VADisplay dpy;
int ftrace_fd;
};
#define LOCK_RESOURCE(pva_trace) \
if(pva_trace) \
pthread_mutex_lock(&pva_trace->resource_mutex)
#define UNLOCK_RESOURCE(pva_trace) \
if(pva_trace) \
pthread_mutex_unlock(&pva_trace->resource_mutex)
#define LOCK_CONTEXT(pva_trace) \
if(pva_trace) \
pthread_mutex_lock(&pva_trace->context_mutex)
#define UNLOCK_CONTEXT(pva_trace) \
if(pva_trace) \
pthread_mutex_unlock(&pva_trace->context_mutex)
#define DPY2TRACECTX(dpy, context, buf_id) \
struct va_trace *pva_trace = NULL; \
struct trace_context *trace_ctx = NULL; \
VAContextID ctx_id = context; \
\
pva_trace = (struct va_trace *)(((VADisplayContextP)dpy)->vatrace); \
if(!pva_trace) \
return; \
\
if(ctx_id == VA_INVALID_ID) { \
if(buf_id != VA_INVALID_ID) \
ctx_id = get_ctx_by_buf(pva_trace, buf_id); \
else \
return; \
} \
\
if (ctx_id != VA_INVALID_ID) { \
int temp_idx = get_valid_ctx_idx(pva_trace, ctx_id); \
if(temp_idx < MAX_TRACE_CTX_NUM) \
trace_ctx = pva_trace->ptra_ctx[temp_idx]; \
} \
\
if(!trace_ctx \
|| trace_ctx->trace_context != context) { \
return; \
} \
refresh_log_file(pva_trace, trace_ctx)
#define DPY2TRACE_VIRCTX(dpy) \
struct va_trace *pva_trace = NULL; \
struct trace_context *trace_ctx = NULL; \
\
pva_trace = (struct va_trace *)(((VADisplayContextP)dpy)->vatrace); \
if(!pva_trace) \
return; \
\
LOCK_CONTEXT(pva_trace); \
trace_ctx = pva_trace->ptra_ctx[MAX_TRACE_CTX_NUM]; \
if(!trace_ctx) { \
UNLOCK_CONTEXT(pva_trace); \
return; \
} \
refresh_log_file(pva_trace, trace_ctx)
#define DPY2TRACE_VIRCTX_EXIT(pva_trace) \
UNLOCK_CONTEXT(pva_trace)
#define TRACE_FUNCNAME(idx) va_TraceMsg(trace_ctx, "==========%s\n", __func__);
#define TRACE_NEWLINE() do { \
va_TracePrint(trace_ctx, "\n"); \
va_TraceMsg(trace_ctx, "") ; \
} while (0)
#define VA_TRACE_MAX_SIZE (1024)
#define VA_TRACE_HEADER_SIZE (sizeof(uint32_t)*3)
#define VA_TRACE_ID (0x45544156) // VATraceEvent in little endian
#define FTRACE_ENTRY "/sys/kernel/debug/tracing/trace_marker_raw"
VAStatus vaBufferInfo(
VADisplay dpy,
VAContextID context, /* in */
VABufferID buf_id, /* in */
VABufferType *type, /* out */
unsigned int *size, /* out */
unsigned int *num_elements /* out */
);
VAStatus vaLockSurface(VADisplay dpy,
VASurfaceID surface,
unsigned int *fourcc, /* following are output argument */
unsigned int *luma_stride,
unsigned int *chroma_u_stride,
unsigned int *chroma_v_stride,
unsigned int *luma_offset,
unsigned int *chroma_u_offset,
unsigned int *chroma_v_offset,
unsigned int *buffer_name,
void **buffer
);
VAStatus vaUnlockSurface(VADisplay dpy,
VASurfaceID surface
);
static int get_valid_config_idx(
struct va_trace *pva_trace,
VAConfigID config_id)
{
int idx = MAX_TRACE_CTX_NUM;
LOCK_RESOURCE(pva_trace);
for (idx = 0; idx < MAX_TRACE_CTX_NUM; idx++) {
if (pva_trace->config_info[idx].valid &&
pva_trace->config_info[idx].config_id == config_id)
break;
}
UNLOCK_RESOURCE(pva_trace);
return idx;
}
static void add_trace_config_info(
struct va_trace *pva_trace,
VAConfigID config_id,
VAProfile profile,
VAEntrypoint entrypoint)
{
struct trace_config_info *pconfig_info;
int idx = 0;
pid_t thd_id = va_gettid();
LOCK_RESOURCE(pva_trace);
for (idx = 0; idx < MAX_TRACE_CTX_NUM; idx++) {
if (!pva_trace->config_info[idx].valid ||
pva_trace->config_info[idx].config_id == config_id)
break;
}
if (idx < MAX_TRACE_CTX_NUM) {
pconfig_info = &pva_trace->config_info[idx];
pconfig_info->valid = 1;
pconfig_info->config_id = config_id;
pconfig_info->trace_profile = profile;
pconfig_info->trace_entrypoint = entrypoint;
pconfig_info->created_thd_id = thd_id;
}
UNLOCK_RESOURCE(pva_trace);
}
static void delete_trace_config_info(
struct va_trace *pva_trace,
VAConfigID config_id)
{
struct trace_config_info *pconfig_info;
int idx = 0;
LOCK_RESOURCE(pva_trace);
for (idx = 0; idx < MAX_TRACE_CTX_NUM; idx++) {
if (pva_trace->config_info[idx].valid &&
pva_trace->config_info[idx].config_id == config_id)
break;
}
if (idx < MAX_TRACE_CTX_NUM) {
pconfig_info = &pva_trace->config_info[idx];
pconfig_info->valid = 0;
pconfig_info->config_id = -1;
}
UNLOCK_RESOURCE(pva_trace);
}
static VAContextID get_ctx_by_buf(
struct va_trace *pva_trace,
VABufferID buf_id)
{
struct trace_buf_manager *pbuf_mgr = &pva_trace->buf_manager;
struct trace_buf_info *pbuf_info = pbuf_mgr->pbuf_info[0];
VAContextID context = VA_INVALID_ID;
int i = 0, idx = 0;
LOCK_RESOURCE(pva_trace);
idx = buf_id & TRACE_BUF_INFO_HASH_ID_MASK;
for (i = 0; i < MAX_TRACE_BUF_INFO_HASH_LEVEL; i++) {
pbuf_info = pbuf_mgr->pbuf_info[i];
if (!pbuf_info)
break;
if (pbuf_info[idx].valid
&& pbuf_info[idx].buf_id == buf_id) {
context = pbuf_info[idx].ctx_id;
break;
}
}
UNLOCK_RESOURCE(pva_trace);
return context;
}
static void add_trace_buf_info(
struct va_trace *pva_trace,
VAContextID context,
VABufferID buf_id)
{
struct trace_buf_manager *pbuf_mgr = &pva_trace->buf_manager;
struct trace_buf_info *pbuf_info = NULL;
int i = 0, idx = 0;
LOCK_RESOURCE(pva_trace);
idx = buf_id & TRACE_BUF_INFO_HASH_ID_MASK;
for (i = 0; i < MAX_TRACE_BUF_INFO_HASH_LEVEL; i++) {
pbuf_info = pbuf_mgr->pbuf_info[i];
if (!pbuf_info) {
pbuf_info = (struct trace_buf_info *)calloc(
sizeof(struct trace_buf_info) * MAX_TRACE_BUF_INFO_HASH_SIZE,
1);
if (!pbuf_info)
break;
pbuf_mgr->pbuf_info[i] = pbuf_info;
}
if (pbuf_info[idx].valid
&& pbuf_info[idx].buf_id != buf_id)
continue;
pbuf_info[idx].buf_id = buf_id;
pbuf_info[idx].ctx_id = context;
pbuf_info[idx].valid = 1;
break;
}
if (i >= MAX_TRACE_BUF_INFO_HASH_LEVEL)
va_errorMessage(pva_trace->dpy, "Add buf info failed\n");
UNLOCK_RESOURCE(pva_trace);
}
static void delete_trace_buf_info(
struct va_trace *pva_trace,
VABufferID buf_id)
{
struct trace_buf_manager *pbuf_mgr = &pva_trace->buf_manager;
struct trace_buf_info *pbuf_info = NULL;
int i = 0, idx = 0;
LOCK_RESOURCE(pva_trace);
idx = buf_id & TRACE_BUF_INFO_HASH_ID_MASK;
for (i = 0; i < MAX_TRACE_BUF_INFO_HASH_LEVEL; i++) {
pbuf_info = pbuf_mgr->pbuf_info[i];
if (!pbuf_info)
break;
if (pbuf_info[idx].valid
&& pbuf_info[idx].buf_id == buf_id) {
pbuf_info[idx].valid = 0;
break;
}
}
UNLOCK_RESOURCE(pva_trace);
}
/*
static void delete_trace_all_context_buf(
struct va_trace *pva_trace,
VAContextID context)
{
struct trace_buf_manager *pbuf_mgr = &pva_trace->buf_manager;
struct trace_buf_info *pbuf_info = NULL;
int i = 0, j = 0;
LOCK_RESOURCE(pva_trace);
for(i = 0;i < MAX_TRACE_BUF_INFO_HASH_LEVEL;i++) {
pbuf_info = pbuf_mgr->pbuf_info[i];
if(!pbuf_info)
break;
for(j = 0;j < MAX_TRACE_BUF_INFO_HASH_SIZE;j++)
if(pbuf_info[j].valid
&& pbuf_info[j].ctx_id == context)
pbuf_info[j].valid = 0;
}
UNLOCK_RESOURCE(pva_trace);
}
*/
static int get_free_ctx_idx(
struct va_trace *pva_trace,
VAContextID context)
{
int idx;
LOCK_RESOURCE(pva_trace);
for (idx = 0; idx < MAX_TRACE_CTX_NUM; idx++)
if (!pva_trace->ptra_ctx[idx] ||
pva_trace->ptra_ctx[idx]->trace_context == context)
break;
UNLOCK_RESOURCE(pva_trace);
return idx;
}
static int get_valid_ctx_idx(
struct va_trace *pva_trace,
VAContextID context)
{
int idx;
LOCK_RESOURCE(pva_trace);
for (idx = 0; idx < MAX_TRACE_CTX_NUM; idx++)
if (pva_trace->ptra_ctx[idx] &&
pva_trace->ptra_ctx[idx]->trace_context == context)
break;
UNLOCK_RESOURCE(pva_trace);
return idx;
}
static void FILE_NAME_SUFFIX(
char *env_value,
int max_size,
char *suffix_str,
unsigned int suffix_handle)
{
int tmp = strnlen(env_value, max_size);
int left = max_size - tmp;
struct timeval tv;
int size = 0;
if (suffix_str)
size = strlen(suffix_str);
if (left < (size + 8 + 10))
return;
if (gettimeofday(&tv, NULL) == 0) {
sprintf(env_value + tmp,
".%02d%02d%02d.",
(unsigned int)(tv.tv_sec / 3600) % 24,
(unsigned int)(tv.tv_sec / 60) % 60,
(unsigned int)tv.tv_sec % 60);
tmp += 8;
left -= 8;
}
if (suffix_str) {
strcat(env_value + tmp,
suffix_str);
tmp += size;
left -= size;
}
if (suffix_handle) {
sprintf(env_value + tmp,
"0x%08x",
suffix_handle);
}
}
static int open_tracing_specil_file(
struct va_trace *pva_trace,
struct trace_context *ptra_ctx,
int type) // 0: codedbuf, 1: surface
{
char *fn_env = type == 0 ?
pva_trace->fn_codedbuf_env : pva_trace->fn_surface_env;
char env_value[1024];
FILE *fp = NULL;
strncpy(env_value, fn_env, 1024);
env_value[1023] = '\0';
FILE_NAME_SUFFIX(env_value, 1024,
"ctx-", (unsigned int)ptra_ctx->trace_context);
fn_env = strdup(env_value);
if (!fn_env)
return -1;
fp = fopen(fn_env, "w");
if (!fp) {
free(fn_env);
return -1;
}
if (type == 0) {
ptra_ctx->trace_codedbuf_fn = fn_env;
ptra_ctx->trace_fp_codedbuf = fp;
va_infoMessage(pva_trace->dpy, "LIBVA_TRACE_CODEDBUF is on, save codedbuf into %s\n",
fn_env);
} else {
ptra_ctx->trace_surface_fn = fn_env;
ptra_ctx->trace_fp_surface = fp;
va_infoMessage(pva_trace->dpy, "LIBVA_TRACE_SURFACE is on, save surface into %s\n",
fn_env);
}
return 0;
}
static int open_tracing_log_file(
struct va_trace *pva_trace,
struct trace_log_file *plog_file,
pid_t thd_id)
{
FILE *pfp = NULL;
int new_fn_flag = 0;
if (plog_file->used && plog_file->thread_id != thd_id) {
va_errorMessage(pva_trace->dpy, "Try to open a busy log file occupied by other thread\n");
return -1;
}
if (plog_file->thread_id != thd_id) {
char env_value[1024];
strncpy(env_value, pva_trace->fn_log_env, 1024);
env_value[1023] = '\0';
FILE_NAME_SUFFIX(env_value, 1024,
"thd-", (unsigned int)thd_id);
if (plog_file->fn_log)
free(plog_file->fn_log);
plog_file->fn_log = strdup(env_value);
if (!plog_file->fn_log)
goto FAIL;
new_fn_flag = 1;
}
if (!plog_file->used) {
if (new_fn_flag)
pfp = fopen(plog_file->fn_log, "w");
else
pfp = fopen(plog_file->fn_log, "a");
if (!pfp)
goto FAIL;
va_infoMessage(pva_trace->dpy, "%s %s for the thread 0x%08x\n",
new_fn_flag ? "Open new log file" : "Append to log file",
plog_file->fn_log, thd_id);
plog_file->fp_log = pfp;
plog_file->thread_id = thd_id;
}
plog_file->used++;
return 0;
FAIL:
if (plog_file->fn_log) {
free(plog_file->fn_log);
plog_file->fn_log = NULL;
}
return -1;
}
static int get_log_file_idx_by_thd(
struct trace_log_files_manager *plog_files_mgr,
pid_t thd_id)
{
struct trace_log_file *plog_file = plog_files_mgr->log_file;
int first_free_idx = MAX_TRACE_THREAD_NUM;
int i = 0;
for (i = 0; i < MAX_TRACE_THREAD_NUM; i++) {
if (plog_file[i].thread_id == thd_id)
break;
else if (!plog_file[i].used &&
first_free_idx >= MAX_TRACE_THREAD_NUM)
first_free_idx = i;
}
if (i >= MAX_TRACE_THREAD_NUM)
i = first_free_idx;
return i;
}
static struct trace_log_file *start_tracing2log_file(
struct va_trace *pva_trace)
{
struct trace_log_files_manager *plog_files_mgr = NULL;
struct trace_log_file *plog_file = NULL;
pid_t thd_id = va_gettid();
int i = 0;
LOCK_RESOURCE(pva_trace);
plog_files_mgr = &pva_trace->log_files_manager;
i = get_log_file_idx_by_thd(plog_files_mgr, thd_id);
if (i < MAX_TRACE_THREAD_NUM) {
plog_file = &plog_files_mgr->log_file[i];
if (open_tracing_log_file(pva_trace, plog_file, thd_id) < 0) {
plog_file = NULL;
}
}
UNLOCK_RESOURCE(pva_trace);
return plog_file;
}
static void stop_tracing2log_file(
struct va_trace *pva_trace,
struct trace_log_file *plog_file)
{
LOCK_RESOURCE(pva_trace);
if (--plog_file->used <= 0) {
if (plog_file->fp_log) {
fclose(plog_file->fp_log);
plog_file->fp_log = NULL;
}
}
UNLOCK_RESOURCE(pva_trace);
}
static void refresh_log_file(
struct va_trace *pva_trace,
struct trace_context *ptra_ctx)
{
struct trace_log_file *plog_file = NULL;
pid_t thd_id = va_gettid();
int i = 0;
plog_file = ptra_ctx->plog_file;
if (plog_file && plog_file->thread_id != thd_id) {
plog_file = start_tracing2log_file(pva_trace);
if (plog_file) {
int first_free_idx = -1;
ptra_ctx->plog_file = plog_file;
for (i = 0; i < MAX_TRACE_THREAD_NUM; i++) {
if (!ptra_ctx->plog_file_list[i]) {
if (first_free_idx < 0)
first_free_idx = i;
} else if (ptra_ctx->plog_file_list[i]->thread_id == thd_id)
break;
}
if (i > MAX_TRACE_THREAD_NUM
&& first_free_idx >= 0)
ptra_ctx->plog_file_list[first_free_idx] = plog_file;
}
}
}
void va_TraceInit(VADisplay dpy)
{
char env_value[1024];
struct va_trace *pva_trace = calloc(sizeof(struct va_trace), 1);
struct trace_context *trace_ctx = calloc(sizeof(struct trace_context), 1);
if (pva_trace == NULL || trace_ctx == NULL) {
free(pva_trace);
free(trace_ctx);
return;
}
pva_trace->dpy = dpy;
pva_trace->ftrace_fd = -1;
pthread_mutex_init(&pva_trace->resource_mutex, NULL);
pthread_mutex_init(&pva_trace->context_mutex, NULL);
if (va_parseConfig("LIBVA_TRACE", &env_value[0]) == 0) {
pva_trace->fn_log_env = strdup(env_value);
if (strcmp(env_value, "FTRACE") == 0) {
pva_trace->ftrace_fd = open(FTRACE_ENTRY, O_WRONLY);
if (pva_trace->ftrace_fd >= 0) {
va_trace_flag = VA_TRACE_FLAG_FTRACE;
va_infoMessage(dpy, "LIBVA_TRACE is active in ftrace mode, use trace-cmd to capture\n");
} else {
va_errorMessage(dpy, "Open ftrace entry failed (%s)\n", strerror(errno));
}
} else {
trace_ctx->plog_file = start_tracing2log_file(pva_trace);
if (trace_ctx->plog_file) {
trace_ctx->plog_file_list[0] = trace_ctx->plog_file;
va_trace_flag = VA_TRACE_FLAG_LOG;
va_infoMessage(dpy, "LIBVA_TRACE is on, save log into %s\n",
trace_ctx->plog_file->fn_log);
} else {
va_errorMessage(dpy, "Open file %s failed (%s)\n", env_value, strerror(errno));
}
}
}
/* may re-get the global settings for multiple context */
if ((va_trace_flag & (VA_TRACE_FLAG_LOG | VA_TRACE_FLAG_FTRACE)) && (va_parseConfig("LIBVA_TRACE_BUFDATA", NULL) == 0)) {
va_trace_flag |= VA_TRACE_FLAG_BUFDATA;
va_infoMessage(dpy, "LIBVA_TRACE_BUFDATA is on, dump buffer into log file\n");
}
/* per-context setting */
if (va_parseConfig("LIBVA_TRACE_CODEDBUF", &env_value[0]) == 0) {
pva_trace->fn_codedbuf_env = strdup(env_value);
va_trace_flag |= VA_TRACE_FLAG_CODEDBUF;
}
if (va_parseConfig("LIBVA_TRACE_SURFACE", &env_value[0]) == 0) {
pva_trace->fn_surface_env = strdup(env_value);
/* for surface data dump, it is time-consume, and may
* cause some side-effect, so only trace the needed surfaces
* to trace encode surface, set the trace file name to sth like *enc*
* to trace decode surface, set the trace file name to sth like *dec*
* if no dec/enc in file name, set both
*/
if (strstr(env_value, "dec"))
va_trace_flag |= VA_TRACE_FLAG_SURFACE_DECODE;
if (strstr(env_value, "enc"))
va_trace_flag |= VA_TRACE_FLAG_SURFACE_ENCODE;
if (strstr(env_value, "jpeg") || strstr(env_value, "jpg"))
va_trace_flag |= VA_TRACE_FLAG_SURFACE_JPEG;
if (va_parseConfig("LIBVA_TRACE_SURFACE_GEOMETRY", &env_value[0]) == 0) {
char *p = env_value, *q;
trace_ctx->trace_surface_width = (unsigned int) strtod(p, &q);
p = q + 1; /* skip "x" */
trace_ctx->trace_surface_height = (unsigned int) strtod(p, &q);
p = q + 1; /* skip "+" */
trace_ctx->trace_surface_xoff = (unsigned int) strtod(p, &q);
p = q + 1; /* skip "+" */
trace_ctx->trace_surface_yoff = (unsigned int) strtod(p, &q);
va_infoMessage(dpy, "LIBVA_TRACE_SURFACE_GEOMETRY is on, only dump surface %dx%d+%d+%d content\n",
trace_ctx->trace_surface_width,
trace_ctx->trace_surface_height,
trace_ctx->trace_surface_xoff,
trace_ctx->trace_surface_yoff);
}
}
trace_ctx->trace_context = VA_INVALID_ID;
pva_trace->ptra_ctx[MAX_TRACE_CTX_NUM] = trace_ctx;
((VADisplayContextP)dpy)->vatrace = (void *)pva_trace;
if (!va_trace_flag)
va_TraceEnd(dpy);
}
void va_TraceEnd(VADisplay dpy)
{
struct va_trace *pva_trace = NULL;
int i = 0;
pva_trace = (struct va_trace *)(((VADisplayContextP)dpy)->vatrace);
if (!pva_trace)
return;
if (pva_trace->fn_log_env)
free(pva_trace->fn_log_env);
if (pva_trace->fn_codedbuf_env)
free(pva_trace->fn_codedbuf_env);
if (pva_trace->fn_surface_env)
free(pva_trace->fn_surface_env);
for (i = 0; i < MAX_TRACE_BUF_INFO_HASH_LEVEL; i++)
if (pva_trace->buf_manager.pbuf_info[i])
free(pva_trace->buf_manager.pbuf_info[i]);
for (i = 0; i < MAX_TRACE_THREAD_NUM; i++) {
struct trace_log_file *plog_file = NULL;
plog_file = &pva_trace->log_files_manager.log_file[i];
if (plog_file) {
if (plog_file->fn_log)
free(plog_file->fn_log);
if (plog_file->fp_log)
fclose(plog_file->fp_log);
}
}
for (i = 0; i < MAX_TRACE_CTX_NUM; i++) {
struct trace_context *trace_ctx = NULL;
if (pva_trace->context_num <= 0)
break;
trace_ctx = pva_trace->ptra_ctx[i];
if (trace_ctx) {
if (trace_ctx->trace_codedbuf_fn)
free(trace_ctx->trace_codedbuf_fn);
if (trace_ctx->trace_fp_codedbuf)
fclose(trace_ctx->trace_fp_codedbuf);
if (trace_ctx->trace_surface_fn)
free(trace_ctx->trace_surface_fn);
if (trace_ctx->trace_fp_surface)
fclose(trace_ctx->trace_fp_surface);
free(pva_trace->ptra_ctx[i]);
pva_trace->context_num--;
}
}
free(pva_trace->ptra_ctx[MAX_TRACE_CTX_NUM]);
// close ftrace file if have
if (pva_trace->ftrace_fd >= 0) {
close(pva_trace->ftrace_fd);
}
pva_trace->dpy = NULL;
pthread_mutex_destroy(&pva_trace->resource_mutex);
pthread_mutex_destroy(&pva_trace->context_mutex);
free(pva_trace);
((VADisplayContextP)dpy)->vatrace = NULL;
}
static void va_TraceVPrint(struct trace_context *trace_ctx, const char *msg, va_list args)
{
FILE *fp = NULL;
if (!(va_trace_flag & VA_TRACE_FLAG_LOG)
|| !trace_ctx->plog_file)
return;
fp = trace_ctx->plog_file->fp_log;
if (msg) {
vfprintf(fp, msg, args);
} else
fflush(fp);
}
static void va_TracePrint(struct trace_context *trace_ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
va_TraceVPrint(trace_ctx, msg, args);
va_end(args);
}
static void va_TraceMsg(struct trace_context *trace_ctx, const char *msg, ...)
{
va_list args;
struct timeval tv;
if (!msg) {
va_TracePrint(trace_ctx, msg);
return;
}
if (gettimeofday(&tv, NULL) == 0)
va_TracePrint(trace_ctx, "[%04d.%06d]",
(unsigned int)tv.tv_sec & 0xffff, (unsigned int)tv.tv_usec);
if (trace_ctx->trace_context != VA_INVALID_ID)
va_TracePrint(trace_ctx,
"[ctx 0x%08x]", trace_ctx->trace_context);
else
va_TracePrint(trace_ctx, "[ctx none]");
va_start(args, msg);
va_TraceVPrint(trace_ctx, msg, args);
va_end(args);
}
static void va_TraceSurface(VADisplay dpy, VAContextID context)
{
unsigned int i;
unsigned int fourcc; /* following are output argument */