File: pmt_util.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (110 lines) | stat: -rw-r--r-- 3,471 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2021-2023 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/os_interface/linux/pmt_util.h"

#include "shared/source/os_interface/linux/file_descriptor.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include "shared/source/utilities/directory.h"

#include <climits>

#include <algorithm>
#include <array>
#include <cstdint>
#include <fcntl.h>
#include <map>
#include <sstream>
#include <string_view>

namespace NEO {

void PmtUtil::getTelemNodesInPciPath(std::string_view rootPciPath, std::map<uint32_t, std::string> &telemPciPath) {

    // if bdf = 0000:3a:00.0
    // link = "../../devices/pci0000:37/0000:37:01.0/0000:38:00.0/0000:39:01.0/0000:3a:00.0/drm/renderD128/",
    // then root path = "/pci0000:37/0000:37:01.0"
    auto telemNodeList = Directory::getFiles("/sys/class/intel_pmt");
    std::string_view telemDirString("/sys/class/intel_pmt/telem");
    telemPciPath.clear();

    // Get the telem nodes under the same root pci path
    for (auto &telemNode : telemNodeList) {
        char symlink[256] = {'\0'};
        std::size_t pos = telemNode.find(telemDirString);
        if (pos != std::string::npos) {
            pos += telemDirString.length();
            int len = SysCalls::readlink(telemNode.c_str(), symlink, sizeof(symlink) - 1);
            if (len != -1) {
                symlink[len] = '\0';
                std::string_view symlinkString(symlink);
                if (symlinkString.find(rootPciPath.data()) != std::string::npos) {
                    int index = std::stoi(telemNode.substr(pos));
                    telemPciPath[index] = telemNode;
                }
            }
        }
    }
}

bool PmtUtil::readGuid(std::string_view telemDir, std::array<char, PmtUtil::guidStringSize> &guidString) {
    std::ostringstream guidFilename;
    guidFilename << telemDir << "/guid";
    auto fd = FileDescriptor(guidFilename.str().c_str(), O_RDONLY);
    ssize_t bytesRead = 0;
    if (fd > 0) {
        guidString.fill('\0');
        bytesRead = SysCalls::pread(fd, guidString.data(), guidString.size() - 1, 0);
    }
    if (bytesRead <= 0) {
        return false;
    }
    std::replace(guidString.begin(), guidString.end(), '\n', '\0');
    return true;
}

bool PmtUtil::readOffset(std::string_view telemDir, uint64_t &offset) {

    std::ostringstream offsetFilename;
    offsetFilename << telemDir << "/offset";
    auto fd = FileDescriptor(offsetFilename.str().c_str(), O_RDONLY);
    ssize_t bytesRead = 0;
    std::array<char, 16> offsetString = {'\0'};
    if (fd > 0) {
        offset = ULONG_MAX;
        bytesRead = SysCalls::pread(fd, offsetString.data(), offsetString.size() - 1, 0);
    }
    if (bytesRead <= 0) {
        return false;
    }

    std::replace(offsetString.begin(), offsetString.end(), '\n', '\0');
    offset = std::strtoul(offsetString.data(), nullptr, 10);

    if (offset == ULONG_MAX) {
        return false;
    }
    return true;
}

ssize_t PmtUtil::readTelem(std::string_view telemDir, const std::size_t count, const uint64_t offset, void *data) {

    if (data == nullptr) {
        return 0;
    }

    ssize_t bytesRead = 0;
    std::ostringstream telemFilename;
    telemFilename << telemDir << "/telem";
    auto fd = FileDescriptor(telemFilename.str().c_str(), O_RDONLY);
    if (fd > 0) {
        bytesRead = SysCalls::pread(fd, data, count, static_cast<off_t>(offset));
    }
    return bytesRead;
}

} // namespace NEO