-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathvgpu_manager.cpp
1277 lines (1195 loc) · 53.8 KB
/
vgpu_manager.cpp
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) 2023 Intel Corporation
* SPDX-License-Identifier: MIT
* @file vgpu_manager.cpp
*/
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <algorithm>
#include <sys/stat.h>
#include <iomanip>
#include <dlfcn.h>
#include "./vgpu_manager.h"
#include "core/core.h"
#include "infrastructure/xpum_config.h"
#include "infrastructure/configuration.h"
#include "infrastructure/utility.h"
#include "infrastructure/handle_lock.h"
#include "xpum_api.h"
namespace xpum {
static bool is_path_exist(const std::string &s) {
struct stat buffer;
return (stat(s.c_str(), &buffer) == 0);
}
static uint32_t getVFMaxNumberByPciDeviceId(int deviceId) {
switch (deviceId) {
case 0x56c0:
case 0x56c1:
case 0x56c2:
return 31;
case 0x0bd4:
case 0x0bd5:
case 0x0bd6:
return 62;
case 0x0bda:
case 0x0bdb:
case 0x0b6e:
return 63;
default:
return 0;
}
}
xpum_result_t VgpuManager::createVf(xpum_device_id_t deviceId, xpum_vgpu_config_t* param) {
XPUM_LOG_DEBUG("vgpuCreateVf, {}, {}, {}", deviceId, param->numVfs, param->lmemPerVf);
auto res = vgpuValidateDevice(deviceId);
if (res != XPUM_OK) {
return res;
}
DeviceSriovInfo deviceInfo;
if (!loadSriovData(deviceId, deviceInfo)) {
return XPUM_VGPU_SYSFS_ERROR;
}
std::unique_lock<std::mutex> lock(mutex);
std::string numVfsString;
std::stringstream numVfsPath;
numVfsPath << "/sys/class/drm/" << deviceInfo.drmPath << "/device/sriov_numvfs";
try {
readFile(numVfsPath.str(), numVfsString);
} catch (std::ios::failure &e) {
return XPUM_VGPU_SYSFS_ERROR;
}
if (std::stoi(numVfsString) > 0) {
return XPUM_VGPU_DIRTY_PF;
}
Property prop;
Core::instance().getDeviceManager()->getDevice(std::to_string(deviceId))->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_PCI_DEVICE_ID, prop);
int pciDeviceId = std::stoi(prop.getValue().substr(2), nullptr, 16);
if (param->numVfs == 0 || param->numVfs > getVFMaxNumberByPciDeviceId(pciDeviceId)){
XPUM_LOG_ERROR("Configuration item for {} VFs out of range", param->numVfs);
return XPUM_VGPU_INVALID_NUMVFS;
}
if (deviceInfo.numTiles > 1 && param->numVfs > 1 && param->numVfs % 2 != 0) {
XPUM_LOG_ERROR("Configuration item for {} VFs invalid for two-tiles cards", param->numVfs);
return XPUM_VGPU_INVALID_NUMVFS;
}
AttrFromConfigFile attrs = {};
bool readFlag = readConfigFromFile(deviceId, param->numVfs, attrs);
if (!readFlag) {
return XPUM_VGPU_NO_CONFIG_FILE;
}
if (attrs.vfLmem == 0) {
XPUM_LOG_ERROR("Configuration item for {} VFs not found", param->numVfs);
return XPUM_VGPU_INVALID_NUMVFS;
}
uint64_t lmemToUse = 0;
if (param->lmemPerVf > 0) {
lmemToUse = param->lmemPerVf;
} else if (deviceInfo.eccState == XPUM_ECC_STATE_ENABLED) {
lmemToUse = attrs.vfLmemEcc;
} else {
lmemToUse = attrs.vfLmem;
}
if (deviceInfo.lmemSizeFree < lmemToUse * param->numVfs) {
XPUM_LOG_ERROR("LMEM size too large");
return XPUM_VGPU_INVALID_LMEM;
}
return createVfInternal(deviceInfo, attrs, param->numVfs, lmemToUse) ? XPUM_OK : XPUM_VGPU_CREATE_VF_FAILED;
}
/*
* 1. Get number of VFs
* 2. Get intersted value in the path of PF and each VF
*/
xpum_result_t VgpuManager::getFunctionList(xpum_device_id_t deviceId, std::vector<xpum_vgpu_function_info_t> &result) {
XPUM_LOG_DEBUG("getFunctionList, device id: {}", deviceId);
auto res = vgpuValidateDevice(deviceId);
if (res != XPUM_OK) {
return res;
}
DeviceSriovInfo deviceInfo;
if (!loadSriovData(deviceId, deviceInfo)) {
return XPUM_VGPU_SYSFS_ERROR;
}
std::string numVfsString;
std::string devicePath = std::string("/sys/class/drm/") + deviceInfo.drmPath;
XPUM_LOG_DEBUG("device Path: {}", devicePath);
try {
readFile(devicePath + "/device/sriov_numvfs", numVfsString);
} catch (std::ios::failure &e) {
return XPUM_VGPU_SYSFS_ERROR;
}
int numVfs = std::stoi(numVfsString);
XPUM_LOG_DEBUG("{} VF detected.", numVfs);
/*
* Put PF info into index 0, and VF1..n into index 1..n respectively
*/
for (int functionIndex = 0; functionIndex < numVfs + 1; functionIndex++) {
xpum_vgpu_function_info_t info = {};
info.functionType = (functionIndex == 0 ? DEVICE_FUNCTION_TYPE_PHYSICAL : DEVICE_FUNCTION_TYPE_VIRTUAL);
std::string lmemString, uevent, lmemPath, ueventPath;
if (deviceInfo.deviceModel == XPUM_DEVICE_MODEL_ATS_M_1 || deviceInfo.deviceModel == XPUM_DEVICE_MODEL_ATS_M_3 || deviceInfo.deviceModel == XPUM_DEVICE_MODEL_ATS_M_1G) {
if (functionIndex == 0) {
lmemPath = devicePath + "/iov/pf/gt/available/lmem_free";
} else {
lmemPath = devicePath + "/iov/vf" + std::to_string(functionIndex) + "/gt/lmem_quota";
}
try {
readFile(lmemPath, lmemString);
} catch (std::ios::failure &e) {
return XPUM_VGPU_SYSFS_ERROR;
}
info.lmemSize = std::stoul(lmemString);
} else if (deviceInfo.deviceModel == XPUM_DEVICE_MODEL_PVC) {
for (uint32_t tile = 0; tile < deviceInfo.numTiles; tile++) {
std::string gtNum = std::to_string(tile);
if (functionIndex == 0) {
lmemPath = devicePath + "/iov/pf/gt" + gtNum + "/available/lmem_free";
} else {
lmemPath = devicePath + "/iov/vf" + std::to_string(functionIndex) + "/gt" + gtNum + "/lmem_quota";
}
try {
readFile(lmemPath, lmemString);
} catch (std::ios::failure &e) {
return XPUM_VGPU_SYSFS_ERROR;
}
info.lmemSize += std::stoul(lmemString);
}
} else {
return XPUM_VGPU_UNSUPPORTED_DEVICE_MODEL;
}
if (functionIndex == 0) {
ueventPath = devicePath + "/iov/pf/device/uevent";
} else {
ueventPath = devicePath + "/iov/vf" + std::to_string(functionIndex) + "/device/uevent";
}
info.bdfAddress[0] = 0;
std::ifstream ifs(ueventPath);
std::string line;
while (std::getline(ifs, line)) {
if (line.length() >= XPUM_MAX_STR_LENGTH) {
return XPUM_VGPU_SYSFS_ERROR;
}
sscanf(line.c_str(), "PCI_SLOT_NAME=%s", info.bdfAddress);
if (info.bdfAddress[0] != 0) {
XPUM_LOG_DEBUG("BDF Address: {}", info.bdfAddress);
break;
}
}
std::vector<std::shared_ptr<Device>> deviceList;
Core::instance().getDeviceManager()->getDeviceList(deviceList);
auto target = std::find_if(deviceList.begin(), deviceList.end(), [&info](std::shared_ptr<Device> d) {
Property prop;
d->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_PCI_BDF_ADDRESS, prop);
return prop.getValue().compare(info.bdfAddress) == 0;
});
info.deviceId = target == deviceList.end() ? -1 : std::stoi((*target)->getId());
result.push_back(info);
}
return XPUM_OK;
}
xpum_result_t VgpuManager::removeAllVf(xpum_device_id_t deviceId) {
auto res = vgpuValidateDevice(deviceId);
if (res != XPUM_OK) {
return res;
}
std::unique_lock<std::mutex> lock(mutex);
DeviceSriovInfo deviceInfo;
if (!loadSriovData(deviceId, deviceInfo)) {
return XPUM_VGPU_SYSFS_ERROR;
}
std::stringstream iovPath, numvfsPath;
/*
* Disable all VFs by setting sriov_numvfs to 0
*/
numvfsPath << "/sys/bus/pci/devices/" << deviceInfo.bdfAddress << "/sriov_numvfs";
try {
writeFile(numvfsPath.str(), "0");
} catch (std::ios::failure &e) {
return XPUM_VGPU_REMOVE_VF_FAILED;
}
/*
* Then clear all resources allocated to all VFs
*/
DIR *dir;
dirent *ent;
iovPath << "/sys/class/drm/" << deviceInfo.drmPath << "/iov/";
AttrFromConfigFile zeroAttr = {};
if ((dir = opendir(iovPath.str().c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strstr(ent->d_name, "vf") == NULL) {
continue;
}
try {
if (deviceInfo.deviceModel == XPUM_DEVICE_MODEL_ATS_M_1 || deviceInfo.deviceModel == XPUM_DEVICE_MODEL_ATS_M_3 || deviceInfo.deviceModel == XPUM_DEVICE_MODEL_ATS_M_1G) {
writeVfAttrToSysfs(iovPath.str() + ent->d_name + "/gt", zeroAttr, 0);
} else if (deviceInfo.deviceModel == XPUM_DEVICE_MODEL_PVC) {
for (uint32_t tile = 0; tile < deviceInfo.numTiles; tile++) {
writeVfAttrToSysfs(iovPath.str() + ent->d_name + "/gt" + std::to_string(tile), zeroAttr, 0);
}
}
} catch(std::ios::failure &e) {
closedir(dir);
return XPUM_VGPU_REMOVE_VF_FAILED;
}
}
} else {
XPUM_LOG_ERROR("Failed to open directory {}", iovPath.str());
return XPUM_VGPU_REMOVE_VF_FAILED;
}
closedir(dir);
return XPUM_OK;
}
/*
For VF metrics, calling SYSMAN API with dlopen and duplicating definition of
SYSMAN data structures should be a temperatory solution.
Once XPUM is able to break backward compatibility, e.g., 2.0 release,
these code should be refactored:
1. Call SYSMAN API directly instead of dlopen
2. Remove these duplicated data structure definition
*/
typedef struct _zes_vf_handle_t *zes_vf_handle_t;
typedef struct _zes_vf_exp_capabilities_t
{
zes_structure_type_t stype; ///< [in] type of this structure
void* pNext; ///< [in,out][optional] must be null or a pointer to an extension-specific
///< structure (i.e. contains stype and pNext).
zes_pci_address_t address; ///< [out] Virtual function BDF address
uint32_t vfDeviceMemSize; ///< [out] Virtual function memory size in bytes
uint32_t vfID; ///< [out] Virtual Function ID
} zes_vf_exp_capabilities_t;
typedef struct _zes_vf_util_mem_exp2_t
{
zes_structure_type_t stype; ///< [in] type of this structure
const void* pNext; ///< [in][optional] must be null or a pointer to an extension-specific
///< structure (i.e. contains stype and pNext).
zes_mem_loc_t vfMemLocation; ///< [out] Location of this memory (system, device)
uint64_t vfMemUtilized; ///< [out] Free memory size in bytes.
} zes_vf_util_mem_exp2_t;
typedef struct _zes_vf_util_engine_exp2_t
{
zes_structure_type_t stype; ///< [in] type of this structure
const void* pNext; ///< [in][optional] must be null or a pointer to an extension-specific
///< structure (i.e. contains stype and pNext).
zes_engine_group_t vfEngineType; ///< [out] The engine group.
uint64_t activeCounterValue; ///< [out] Represents active counter.
uint64_t samplingCounterValue; ///< [out] Represents counter value when activeCounterValue was sampled.
///< Refer to the formulae above for calculating the utilization percent
} zes_vf_util_engine_exp2_t;
typedef ze_result_t (*pfnZesDeviceEnumEnabledVfExp_t)(
zes_device_handle_t hDevice, uint32_t *pCount, zes_vf_handle_t *phVFhandle);
typedef ze_result_t (*pfnZesVFManagementGetVFCapabilitiesExp_t)(
zes_vf_handle_t hVFhandle, zes_vf_exp_capabilities_t *pCapability);
typedef ze_result_t (*pfnZesVFManagementGetVFMemoryUtilizationExp2_t)(
zes_vf_handle_t hVFhandle, uint32_t *pCount,
zes_vf_util_mem_exp2_t *pMemUtil);
typedef ze_result_t (*pfnZesVFManagementGetVFEngineUtilizationExp2_t)(
zes_vf_handle_t hVFhandle, uint32_t *pCount,
zes_vf_util_engine_exp2_t *pEngineUtil);
typedef struct {
pfnZesDeviceEnumEnabledVfExp_t pfnZesDeviceEnumEnabledVfExp;
pfnZesVFManagementGetVFCapabilitiesExp_t
pfnZesVFManagementGetVFCapabilitiesExp;
pfnZesVFManagementGetVFMemoryUtilizationExp2_t
pfnZesVFManagementGetVFMemoryUtilizationExp2;
pfnZesVFManagementGetVFEngineUtilizationExp2_t
pfnZesVFManagementGetVFEngineUtilizationExp2 ;
} VfMgmtApi_t;
static xpum_realtime_metric_type_t engineToMetricType(zes_engine_group_t engine) {
xpum_realtime_metric_type_t metricType = XPUM_STATS_MAX;
switch (engine) {
case ZES_ENGINE_GROUP_MEDIA_DECODE_SINGLE:
case ZES_ENGINE_GROUP_MEDIA_ENCODE_SINGLE:
case ZES_ENGINE_GROUP_MEDIA_ENHANCEMENT_SINGLE:
metricType = XPUM_STATS_ENGINE_GROUP_MEDIA_ALL_UTILIZATION;
break;
case ZES_ENGINE_GROUP_COMPUTE_SINGLE:
metricType = XPUM_STATS_ENGINE_GROUP_COMPUTE_ALL_UTILIZATION;
break;
case ZES_ENGINE_GROUP_COPY_SINGLE:
metricType = XPUM_STATS_ENGINE_GROUP_COPY_ALL_UTILIZATION;
break;
case ZES_ENGINE_GROUP_RENDER_SINGLE:
metricType = XPUM_STATS_ENGINE_GROUP_RENDER_ALL_UTILIZATION;
break;
default:
break;
}
return metricType;
}
bool static findVfMgmtApi(VfMgmtApi_t &vfMgmtApi, void *dlHandle) {
vfMgmtApi.pfnZesDeviceEnumEnabledVfExp =
reinterpret_cast<pfnZesDeviceEnumEnabledVfExp_t>(dlsym(dlHandle,
"zesDeviceEnumEnabledVFExp"));
if (vfMgmtApi.pfnZesDeviceEnumEnabledVfExp == nullptr) {
return false;
}
vfMgmtApi.pfnZesVFManagementGetVFCapabilitiesExp =
reinterpret_cast<pfnZesVFManagementGetVFCapabilitiesExp_t>(dlsym(
dlHandle, "zesVFManagementGetVFCapabilitiesExp"));
if (vfMgmtApi.pfnZesVFManagementGetVFCapabilitiesExp == nullptr) {
return false;
}
vfMgmtApi.pfnZesVFManagementGetVFMemoryUtilizationExp2 =
reinterpret_cast<pfnZesVFManagementGetVFMemoryUtilizationExp2_t>(dlsym(
dlHandle, "zesVFManagementGetVFMemoryUtilizationExp2"));
if (vfMgmtApi.pfnZesVFManagementGetVFMemoryUtilizationExp2 == nullptr) {
return false;
}
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2 =
reinterpret_cast<pfnZesVFManagementGetVFEngineUtilizationExp2_t>(dlsym(
dlHandle, "zesVFManagementGetVFEngineUtilizationExp2"));
if (vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2 == nullptr) {
return false;
}
return true;
}
typedef struct {
uint32_t vfid;
zes_vf_handle_t vfh;
zes_vf_exp_capabilities_t cap;
std::vector<zes_vf_util_engine_exp2_t> vues;
} vf_util_snap_t;
static bool getVfEngineUtilWithSnaps(std::vector<xpum_vf_metric_t> &metrics,
std::vector<vf_util_snap_t> &snaps, VfMgmtApi_t &vfMgmtApi,
xpum_device_id_t deviceId, zes_device_handle_t &dh) {
ze_result_t res = ZE_RESULT_SUCCESS;
for (std::size_t i = 0; i < snaps.size(); i++) {
uint32_t veuc = 0;
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2(
snaps[i].vfh, &veuc, nullptr));
if (res != ZE_RESULT_SUCCESS || veuc != snaps[i].vues.size()) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns {} veuc = {}",
res, veuc);
return false;
}
std::vector<zes_vf_util_engine_exp2_t> vues(veuc);
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2(
snaps[i].vfh, &veuc, vues.data()));
if (res != ZE_RESULT_SUCCESS || veuc != snaps[i].vues.size()) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns {} veuc = {}",
res, veuc);
return false;
}
if (vues.size() != snaps[i].vues.size()) {
XPUM_LOG_DEBUG("VF engine number changed");
return false;
}
std::vector<xpum_vf_metric_t> singleGroupMetrics;
// zesVFManagementGetVFEngineUtilizationExp2 returns engine counters
// in same order though it is not documented at the time
for (std::size_t j = 0; j < snaps[i].vues.size(); j++) {
auto vue = vues[j];
if (vue.vfEngineType != snaps[i].vues[j].vfEngineType) {
XPUM_LOG_DEBUG("VF engine type order changed");
return false;
}
if (vue.samplingCounterValue -
snaps[i].vues[j].samplingCounterValue <= 0 ||
vue.activeCounterValue -
snaps[i].vues[j].activeCounterValue < 0) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns invalid values activeCounterValue {}, samplingCounterValue {} and activeCounterValue {}, samplingCounterValue {}",
snaps[i].vues[j].activeCounterValue,
snaps[i].vues[j].samplingCounterValue,
vue.activeCounterValue,
vue.samplingCounterValue);
return false;
}
xpum_vf_metric_t vfm = {};
vfm.metric.metricsType = engineToMetricType(vue.vfEngineType);
vfm.metric.value = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE
* 100 * (vue.activeCounterValue -
snaps[i].vues[j].activeCounterValue) / (
vue.samplingCounterValue -
snaps[i].vues[j].samplingCounterValue);
if (vfm.metric.value >
Configuration::DEFAULT_MEASUREMENT_DATA_SCALE * 100) {
vfm.metric.value =
Configuration::DEFAULT_MEASUREMENT_DATA_SCALE * 100;
}
singleGroupMetrics.push_back(vfm);
XPUM_LOG_TRACE("vfEngineType = {}: activeCounterValue {}, samplingCounterValue {} and activeCounterValue {}, samplingCounterValue {}",
vue.vfEngineType,
snaps[i].vues[j].activeCounterValue,
snaps[i].vues[j].samplingCounterValue,
vue.activeCounterValue,
vue.samplingCounterValue);
}
// Aggregate (by max) utilization per metrics type
uint64_t mediaUtil = UINT64_MAX;
uint64_t copyUtil = UINT64_MAX;
uint64_t renderUtil = UINT64_MAX;
uint64_t computeUtil = UINT64_MAX;
uint64_t allUtil = UINT64_MAX;
for (auto m : singleGroupMetrics) {
switch (m.metric.metricsType) {
case XPUM_STATS_ENGINE_GROUP_MEDIA_ALL_UTILIZATION:
if (mediaUtil == UINT64_MAX) {
mediaUtil = m.metric.value;
} else {
mediaUtil = mediaUtil > m.metric.value ?
mediaUtil : m.metric.value;
}
break;
case XPUM_STATS_ENGINE_GROUP_RENDER_ALL_UTILIZATION:
if (renderUtil == UINT64_MAX) {
renderUtil = m.metric.value;
} else {
renderUtil = renderUtil > m.metric.value ?
renderUtil : m.metric.value;
}
break;
case XPUM_STATS_ENGINE_GROUP_COMPUTE_ALL_UTILIZATION:
if (computeUtil == UINT64_MAX) {
computeUtil = m.metric.value;
} else {
computeUtil = computeUtil > m.metric.value ?
computeUtil : m.metric.value;
}
break;
case XPUM_STATS_ENGINE_GROUP_COPY_ALL_UTILIZATION:
if (copyUtil == UINT64_MAX) {
copyUtil = m.metric.value;
} else {
copyUtil = copyUtil > m.metric.value ?
copyUtil : m.metric.value;
}
break;
default:
XPUM_LOG_DEBUG("unknown VF metric type");
return false;
}
}
if (mediaUtil != UINT64_MAX) {
xpum_vf_metric_t vfm = {};
vfm.deviceId = deviceId;
vfm.vfIndex = snaps[i].vfid;
snprintf(vfm.bdfAddress, XPUM_MAX_STR_LENGTH, "%04x:%02x:%02x.%x",
snaps[i].cap.address.domain, snaps[i].cap.address.bus,
snaps[i].cap.address.device, snaps[i].cap.address.function);
vfm.metric.scale = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE;
vfm.metric.value = mediaUtil;
vfm.metric.metricsType = XPUM_STATS_ENGINE_GROUP_MEDIA_ALL_UTILIZATION;
metrics.push_back(vfm);
XPUM_LOG_TRACE("media overall {}", mediaUtil);
if (allUtil == UINT64_MAX) {
allUtil = mediaUtil;
} else {
allUtil = allUtil > mediaUtil ? allUtil : mediaUtil;
}
}
if (renderUtil != UINT64_MAX) {
xpum_vf_metric_t vfm = {};
vfm.deviceId = deviceId;
vfm.vfIndex = snaps[i].vfid;
snprintf(vfm.bdfAddress, XPUM_MAX_STR_LENGTH, "%04x:%02x:%02x.%x",
snaps[i].cap.address.domain, snaps[i].cap.address.bus,
snaps[i].cap.address.device, snaps[i].cap.address.function);
vfm.metric.scale = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE;
vfm.metric.value = renderUtil;
vfm.metric.metricsType = XPUM_STATS_ENGINE_GROUP_RENDER_ALL_UTILIZATION;
metrics.push_back(vfm);
XPUM_LOG_TRACE("render overall {}", renderUtil);
if (allUtil == UINT64_MAX) {
allUtil = renderUtil;
} else {
allUtil = allUtil > renderUtil ? allUtil : renderUtil;
}
}
if (computeUtil != UINT64_MAX) {
xpum_vf_metric_t vfm = {};
vfm.deviceId = deviceId;
vfm.vfIndex = snaps[i].vfid;
snprintf(vfm.bdfAddress, XPUM_MAX_STR_LENGTH, "%04x:%02x:%02x.%x",
snaps[i].cap.address.domain, snaps[i].cap.address.bus,
snaps[i].cap.address.device, snaps[i].cap.address.function);
vfm.metric.scale = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE;
vfm.metric.value = computeUtil;
vfm.metric.metricsType = XPUM_STATS_ENGINE_GROUP_COMPUTE_ALL_UTILIZATION;
metrics.push_back(vfm);
XPUM_LOG_TRACE("compute overall {}", computeUtil);
if (allUtil == UINT64_MAX) {
allUtil = computeUtil;
} else {
allUtil = allUtil > computeUtil ? allUtil : computeUtil;
}
}
if (copyUtil != UINT64_MAX) {
xpum_vf_metric_t vfm = {};
vfm.deviceId = deviceId;
vfm.vfIndex = snaps[i].vfid;
snprintf(vfm.bdfAddress, XPUM_MAX_STR_LENGTH, "%04x:%02x:%02x.%x",
snaps[i].cap.address.domain, snaps[i].cap.address.bus,
snaps[i].cap.address.device, snaps[i].cap.address.function);
vfm.metric.scale = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE;
vfm.metric.value = copyUtil;
vfm.metric.metricsType = XPUM_STATS_ENGINE_GROUP_COPY_ALL_UTILIZATION;
metrics.push_back(vfm);
XPUM_LOG_TRACE("copy overall {}", copyUtil);
if (allUtil == UINT64_MAX) {
allUtil = copyUtil;
} else {
allUtil = allUtil > copyUtil ? allUtil : copyUtil;
}
}
if (allUtil != UINT64_MAX) {
xpum_vf_metric_t vfm = {};
vfm.deviceId = deviceId;
vfm.vfIndex = snaps[i].vfid;
snprintf(vfm.bdfAddress, XPUM_MAX_STR_LENGTH, "%04x:%02x:%02x.%x",
snaps[i].cap.address.domain, snaps[i].cap.address.bus,
snaps[i].cap.address.device, snaps[i].cap.address.function);
vfm.metric.scale = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE;
vfm.metric.value = allUtil;
vfm.metric.metricsType = XPUM_STATS_GPU_UTILIZATION;
metrics.push_back(vfm);
XPUM_LOG_TRACE("GPU overall {}", allUtil);
}
}
return true;
}
xpum_result_t VgpuManager::getVfMetrics(xpum_device_id_t deviceId,
std::vector<xpum_vf_metric_t> &metrics, uint32_t *count) {
xpum_result_t ret = XPUM_GENERIC_ERROR;
std::vector<vf_util_snap_t> snaps;
auto device = Core::instance().getDeviceManager()->getDevice(
std::to_string(deviceId));
if (device == nullptr) {
return XPUM_RESULT_DEVICE_NOT_FOUND;
}
void *handle = dlopen("libze_loader.so.1", RTLD_NOW);
if (handle == nullptr) {
return XPUM_LEVEL_ZERO_INITIALIZATION_ERROR;
}
VfMgmtApi_t vfMgmtApi;
if (findVfMgmtApi(vfMgmtApi, handle) == false) {
dlclose(handle);
XPUM_LOG_DEBUG("getVfMetrics: findVfMgmtApi returns false");
return XPUM_API_UNSUPPORTED;
}
ze_result_t res = ZE_RESULT_SUCCESS;
auto dh = device->getDeviceHandle();
uint32_t vfCount = 0;
XPUM_ZE_HANDLE_LOCK(dh, res = vfMgmtApi.pfnZesDeviceEnumEnabledVfExp(
dh, &vfCount, nullptr));
if (res != ZE_RESULT_SUCCESS) {
XPUM_LOG_DEBUG("pfnZesDeviceEnumEnabledVfExp returns {} vfCount = {}",
res, vfCount);
dlclose(handle);
return XPUM_GENERIC_ERROR;
}
if (vfCount == 0) {
//check count only
if (count != nullptr) {
*count = 0;
ret = XPUM_OK;
} else {
ret = XPUM_GENERIC_ERROR;
XPUM_LOG_DEBUG("pfnZesDeviceEnumEnabledVfExp vfCount = {}",
vfCount);
}
dlclose(handle);
return ret;
}
std::vector<zes_vf_handle_t> vfs(vfCount);
XPUM_ZE_HANDLE_LOCK(dh, res = vfMgmtApi.pfnZesDeviceEnumEnabledVfExp(
dh, &vfCount, vfs.data()));
if (res != ZE_RESULT_SUCCESS || vfCount == 0) {
XPUM_LOG_DEBUG("pfnZesDeviceEnumEnabledVfExp returns {} vfCount = {}",
res, vfCount);
goto RTN;
}
//check count only
if (count != nullptr) {
uint32_t engineUtilCount = 0;
for (auto vfh : vfs) {
// veuc: VF Engine Util Count
uint32_t veuc = 0;
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2(
vfh, &veuc, nullptr));
if (res != ZE_RESULT_SUCCESS) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns {} veuc = {}",
res, veuc);
goto RTN;
}
std::vector<zes_vf_util_engine_exp2_t> vues(veuc);
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2(
vfh, &veuc, vues.data()));
if (res != ZE_RESULT_SUCCESS) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns {} veuc = {}",
res, veuc);
goto RTN;
}
/*
According to the doc, the 6 single engine groups below are supported
by zesVFManagementGetVFEngineUtilizationExp2
https://docs.intel.com/documents/GfxSWAT/Common/sysman/SysmanAPI.html#engine
ZES_ENGINE_GROUP_COMPUTE_SINGLE = 4
ZES_ENGINE_GROUP_RENDER_SINGLE = 5
ZES_ENGINE_GROUP_MEDIA_DECODE_SINGLE = 6
ZES_ENGINE_GROUP_MEDIA_ENCODE_SINGLE = 7
ZES_ENGINE_GROUP_COPY_SINGLE = 8
ZES_ENGINE_GROUP_MEDIA_ENHANCEMENT_SINGLE = 9
The single engine groups would be aggregated (max) to 4 overall
engine groups: media, compute, copy, and render. And these 4
would be aggreated to overall GPU util, then the expected engine
count is 4+1=5 for each VF
*/
/*
The varialbe would be set to 1 if the corresponding single engine
gropup is found. If multiple engine instance for a single engine
group exists, it is still 1 because the data would be aggregaed
*/
uint32_t mediaEngine = 0;
uint32_t computeEngine = 0;
uint32_t copyEngine = 0;
uint32_t renderEngine = 0;
for (auto vue : vues) {
if (vue.vfEngineType == ZES_ENGINE_GROUP_MEDIA_DECODE_SINGLE ||
vue.vfEngineType == ZES_ENGINE_GROUP_MEDIA_ENCODE_SINGLE ||
vue.vfEngineType ==
ZES_ENGINE_GROUP_MEDIA_ENHANCEMENT_SINGLE) {
mediaEngine = 1;
continue;
}
if (vue.vfEngineType == ZES_ENGINE_GROUP_COMPUTE_SINGLE) {
computeEngine = 1;
continue;
}
if (vue.vfEngineType == ZES_ENGINE_GROUP_RENDER_SINGLE) {
renderEngine = 1;
continue;
}
if (vue.vfEngineType == ZES_ENGINE_GROUP_COPY_SINGLE) {
copyEngine = 1;
continue;
}
}
uint32_t allEngine = mediaEngine + computeEngine + copyEngine +
renderEngine;
if (allEngine > 0) {
// The four aggregated engine group will be aggregated to
// overall GPU util
allEngine++;
}
engineUtilCount += allEngine;
}
// Add vfCount because there would be memory util for each VF
*count = engineUtilCount + vfCount;
ret = XPUM_OK;
goto RTN;
}
for (auto vfh : vfs) {
zes_vf_exp_capabilities_t cap = {};
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFCapabilitiesExp(vfh, &cap));
if (res != ZE_RESULT_SUCCESS || cap.vfDeviceMemSize == 0) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFCapabilitiesExp returns {}",
res);
goto RTN;
}
uint32_t mc = 0;
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFMemoryUtilizationExp2(vfh, &mc,
nullptr));
if (res != ZE_RESULT_SUCCESS) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFMemoryUtilizationExp2 returns {}",
res);
goto RTN;
}
std::vector<zes_vf_util_mem_exp2_t> vums(mc);
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFMemoryUtilizationExp2(vfh, &mc,
vums.data()));
if (res != ZE_RESULT_SUCCESS) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFMemoryUtilizationExp2 returns {}",
res);
goto RTN;
}
// vmu: VF Memory Utilized
uint64_t vmu = UINT64_MAX;
for (auto mu : vums) {
if (mu.vfMemLocation == ZES_MEM_LOC_DEVICE) {
vmu = mu.vfMemUtilized;
}
}
if (vmu == UINT64_MAX) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFMemoryUtilizationExp2 returns no ZES_MEM_LOC_DEVICE");
goto RTN;
}
xpum_vf_metric_t vfm = {};
vfm.deviceId = deviceId;
snprintf(vfm.bdfAddress, XPUM_MAX_STR_LENGTH, "%04x:%02x:%02x.%x",
cap.address.domain, cap.address.bus,
cap.address.device, cap.address.function);
if (getVfId(vfm.vfIndex, vfm.bdfAddress, XPUM_MAX_STR_LENGTH,
deviceId) == false) {
XPUM_LOG_DEBUG("VF index cannot be found for bdf {}",
vfm.bdfAddress);
}
vfm.metric.metricsType = XPUM_STATS_MEMORY_UTILIZATION;
vfm.metric.scale = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE;
vfm.metric.value = Configuration::DEFAULT_MEASUREMENT_DATA_SCALE * 100 *
vmu / cap.vfDeviceMemSize;
metrics.push_back(vfm);
uint32_t veuc = 0;
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2(
vfh, &veuc, nullptr));
if (res != ZE_RESULT_SUCCESS || veuc == 0) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns {} veuc = {}",
res, veuc);
goto RTN;
}
vf_util_snap_t snap;
snap.vfid = vfm.vfIndex;
snap.vfh = vfh;
snap.cap = cap;
snap.vues = std::vector<zes_vf_util_engine_exp2_t>(veuc);
XPUM_ZE_HANDLE_LOCK(dh, res =
vfMgmtApi.pfnZesVFManagementGetVFEngineUtilizationExp2(
vfh, &veuc, snap.vues.data()));
if (res != ZE_RESULT_SUCCESS || veuc == 0) {
XPUM_LOG_DEBUG("pfnZesVFManagementGetVFEngineUtilizationExp2 returns {} veuc = {}",
res, veuc);
goto RTN;
}
snaps.push_back(snap);
}
std::this_thread::sleep_for(std::chrono::milliseconds(
Configuration::VF_METRICS_INTERVAL));
if (getVfEngineUtilWithSnaps(metrics, snaps, vfMgmtApi, deviceId, dh)
== true) {
ret = XPUM_OK;
}
RTN:
dlclose(handle);
return ret;
}
bool VgpuManager::getVfBdf(char *bdf, uint32_t szBdf, uint32_t vfIndex,
xpum_device_id_t deviceId) {
//BDF Format in uevent is cccc:cc:cc.c
#define BDF_SIZE 12
if (bdf == nullptr || szBdf < BDF_SIZE + 1) {
return false;
}
auto device = Core::instance().getDeviceManager()->getDevice(
std::to_string(deviceId));
if (device == nullptr) {
return false;
}
Property prop = {};
if (device->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_DRM_DEVICE, prop)
== false) {
return false;
}
std::string str = prop.getValue();
std::string::size_type n = str.rfind('/');
if (n == std::string::npos || n == str.length() - 1) {
return false;
}
str = str.substr(n);
str = "/sys/class/drm/" + str + "/iov/vf" + std::to_string(vfIndex) +
"/device/uevent";
std::ifstream ifs(str);
if (ifs.is_open() == false) {
XPUM_LOG_DEBUG("cannot open uevent file = {}", str);
return false;
}
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
std::string key = "PCI_SLOT_NAME=";
n = content.find(key);
if (n == std::string::npos ||
n + key.length() + BDF_SIZE > content.length() - 1) {
XPUM_LOG_DEBUG("uevent offset error");
return false;
}
str = content.substr(n + key.length(), BDF_SIZE);
str.copy(bdf, BDF_SIZE);
bdf[BDF_SIZE] = '\0';
return true;
}
bool VgpuManager::getVfId(uint32_t &vfIndex, const char *bdf, uint32_t szBdf,
xpum_device_id_t deviceId) {
//BDF Format in uevent is cccc:cc:cc.c
if (bdf == nullptr || szBdf < BDF_SIZE + 1) {
return false;
}
bool ret = false;
auto device = Core::instance().getDeviceManager()->getDevice(
std::to_string(deviceId));
if (device == nullptr) {
return false;
}
Property prop = {};
if (device->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_DRM_DEVICE, prop)
== false) {
return false;
}
std::string str = prop.getValue();
std::string::size_type n = str.rfind('/');
if (n == std::string::npos || n == str.length() - 1) {
return false;
}
str = str.substr(n);
str = "/sys/class/drm/" + str + "/iov/";
DIR *pdir = NULL;
struct dirent *pdirent = NULL;
pdir = opendir(str.c_str());
if (pdir != NULL) {
while ((pdirent = readdir(pdir)) != NULL) {
if (pdirent->d_name[0] == '.') {
continue;
}
if (strncmp(pdirent->d_name, "vf", 2) != 0) {
continue;
}
std::string ueventFN = str + pdirent->d_name + "/device/uevent";
std::ifstream ifs(ueventFN);
if (ifs.is_open() == false) {
XPUM_LOG_DEBUG("cannot open uevent file = {}", ueventFN);
continue;
}
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
if (content.find(bdf) != std::string::npos) {
sscanf(pdirent->d_name, "vf%d", &vfIndex);
ret = true;
break;
}
}
closedir(pdir);
}
return ret;
}
bool VgpuManager::loadSriovData(xpum_device_id_t deviceId, DeviceSriovInfo &data) {
auto device = Core::instance().getDeviceManager()->getDevice(std::to_string(deviceId));
Property prop;
data.deviceModel = device->getDeviceModel();
device->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_DRM_DEVICE, prop);
char drm[XPUM_MAX_STR_LENGTH];
if (prop.getValue().length() >= XPUM_MAX_STR_LENGTH) {
return false;
}
sscanf(prop.getValue().c_str(), "/dev/dri/%s", drm);
data.drmPath = std::string(drm);
device->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_PCI_BDF_ADDRESS, prop);
data.bdfAddress = prop.getValue();
device->getProperty(XPUM_DEVICE_PROPERTY_INTERNAL_NUMBER_OF_TILES, prop);
data.numTiles = prop.getValueInt();
bool available;
bool configurable;
xpum_ecc_state_t current, pending;
xpum_ecc_action_t action;
xpumGetEccState(stoi(device->getId()), &available, &configurable, ¤t, &pending, &action);
data.eccState = current;
std::string lmem, ggtt, doorbell, context;
if (data.deviceModel == XPUM_DEVICE_MODEL_ATS_M_1 || data.deviceModel == XPUM_DEVICE_MODEL_ATS_M_3 || data.deviceModel == XPUM_DEVICE_MODEL_ATS_M_1G) {
std::string pfIovPath = std::string("/sys/class/drm/") + drm + "/iov/pf/gt/available/";
try {
readFile(pfIovPath + "lmem_free", lmem);
readFile(pfIovPath + "ggtt_free", ggtt);
readFile(pfIovPath + "doorbells_free", doorbell);
readFile(pfIovPath + "contexts_free", context);
} catch (std::ios::failure &e) {
return false;
}
data.lmemSizeFree = std::stoul(lmem);
data.ggttSizeFree = std::stoul(ggtt);
data.contextFree = std::stoi(context);
data.doorbellFree = std::stoi(doorbell);
} else if (data.deviceModel == XPUM_DEVICE_MODEL_PVC) {
for (uint32_t tile = 0; tile < data.numTiles; tile++) {
std::string pfIovPath = std::string("/sys/class/drm/") + drm + "/iov/pf/gt" + std::to_string(tile) + "/available/";
try {
readFile(pfIovPath + "lmem_free", lmem);
readFile(pfIovPath + "ggtt_free", ggtt);
readFile(pfIovPath + "doorbells_free", doorbell);
readFile(pfIovPath + "contexts_free", context);
} catch (std::ios::failure &e) {
return false;
}
data.lmemSizeFree += std::stoul(lmem);
data.ggttSizeFree += std::stoul(ggtt);
data.contextFree += std::stoi(context);
data.doorbellFree += std::stoi(doorbell);
}
} else {
return false;
}
return true;
}
void VgpuManager::readFile(const std::string& path, std::string& content) {
std::ifstream ifs;
std::stringstream ss;
ifs.exceptions(std::ios::failbit | std::ios::badbit);
try {