File: os_events_imp.cpp

package info (click to toggle)
intel-compute-runtime 22.43.24595.41-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,740 kB
  • sloc: cpp: 631,142; lisp: 3,515; sh: 470; makefile: 76; python: 21
file content (144 lines) | stat: -rw-r--r-- 4,908 bytes parent folder | download
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
/*
 * Copyright (C) 2020-2021 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "level_zero/tools/source/sysman/events/linux/os_events_imp.h"

#include "sysman/events/events_imp.h"
#include "sysman/linux/os_sysman_imp.h"

namespace L0 {

const std::string LinuxEventsImp::varFs("/var/lib/libze_intel_gpu/");
const std::string LinuxEventsImp::detachEvent("remove");
const std::string LinuxEventsImp::attachEvent("add");

bool LinuxEventsImp::isResetRequired(zes_event_type_flags_t &pEvent) {
    zes_device_state_t pState = {};
    pLinuxSysmanImp->getSysmanDeviceImp()->deviceGetState(&pState);
    if (pState.reset) {
        pEvent |= ZES_EVENT_TYPE_FLAG_DEVICE_RESET_REQUIRED;
        return true;
    }
    return false;
}

bool LinuxEventsImp::checkDeviceDetachEvent(zes_event_type_flags_t &pEvent) {
    // When device detach uevent is generated, then L0 udev rules will create a file:
    // /var/lib/libze_intel_gpu/remove-<ID_PATH_TAG>
    // For <ID_PATH_TAG>, check comment in LinuxEventsImp::init()
    const std::string deviceDetachFile = detachEvent + "-" + pciIdPathTag;
    const std::string deviceDetachFileAbsolutePath = varFs + deviceDetachFile;
    uint32_t val = 0;
    auto result = pFsAccess->read(deviceDetachFileAbsolutePath, val);
    if (result != ZE_RESULT_SUCCESS) {
        return false;
    }
    if (val == 1) {
        pEvent |= ZES_EVENT_TYPE_FLAG_DEVICE_DETACH;
        return true;
    }
    return false;
}

bool LinuxEventsImp::checkDeviceAttachEvent(zes_event_type_flags_t &pEvent) {
    // When device detach uevent is generated, then L0 udev rules will create a file:
    // /var/lib/libze_intel_gpu/add-<ID_PATH_TAG>
    // For <ID_PATH_TAG>, check comment in LinuxEventsImp::init()
    const std::string deviceAttachFile = attachEvent + "-" + pciIdPathTag;
    const std::string deviceAttachFileAbsolutePath = varFs + deviceAttachFile;
    uint32_t val = 0;
    auto result = pFsAccess->read(deviceAttachFileAbsolutePath, val);
    if (result != ZE_RESULT_SUCCESS) {
        return false;
    }
    if (val == 1) {
        pEvent |= ZES_EVENT_TYPE_FLAG_DEVICE_ATTACH;
        return true;
    }
    return false;
}

bool LinuxEventsImp::checkIfMemHealthChanged(zes_event_type_flags_t &pEvent) {
    if (currentMemHealth() != memHealthAtEventRegister) {
        pEvent |= ZES_EVENT_TYPE_FLAG_MEM_HEALTH;
        return true;
    }
    return false;
}

bool LinuxEventsImp::eventListen(zes_event_type_flags_t &pEvent, uint64_t timeout) {
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_DEVICE_RESET_REQUIRED) {
        if (isResetRequired(pEvent)) {
            registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_DEVICE_RESET_REQUIRED); //After receiving event unregister it
            return true;
        }
    }
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_DEVICE_DETACH) {
        if (checkDeviceDetachEvent(pEvent)) {
            registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_DEVICE_DETACH);
            return true;
        }
    }
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_DEVICE_ATTACH) {
        if (checkDeviceAttachEvent(pEvent)) {
            registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_DEVICE_ATTACH);
            return true;
        }
    }
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_MEM_HEALTH) {
        if (checkIfMemHealthChanged(pEvent)) {
            registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_MEM_HEALTH);
            return true;
        }
    }
    return false;
}

ze_result_t LinuxEventsImp::eventRegister(zes_event_type_flags_t events) {
    if (0x7fff < events) {
        return ZE_RESULT_ERROR_INVALID_ENUMERATION;
    }
    registeredEvents |= events;
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_MEM_HEALTH) {
        memHealthAtEventRegister = currentMemHealth();
    }
    return ZE_RESULT_SUCCESS;
}

zes_mem_health_t LinuxEventsImp::currentMemHealth() {
    return ZES_MEM_HEALTH_UNKNOWN;
}

void LinuxEventsImp::getPciIdPathTag() {
    std::string bdfDir;
    ze_result_t result = pSysfsAccess->readSymLink("device", bdfDir);
    if (ZE_RESULT_SUCCESS != result) {
        return;
    }
    const auto loc = bdfDir.find_last_of('/');
    auto bdf = bdfDir.substr(loc + 1);
    std::replace(bdf.begin(), bdf.end(), ':', '_');
    std::replace(bdf.begin(), bdf.end(), '.', '_');
    // ID_PATH_TAG key is received when uevent related to device add/remove is generated.
    // Example of ID_PATH_TAG is:
    // ID_PATH_TAG=pci-0000_8c_00_0
    pciIdPathTag = "pci-" + bdf;
}

LinuxEventsImp::LinuxEventsImp(OsSysman *pOsSysman) {
    pLinuxSysmanImp = static_cast<LinuxSysmanImp *>(pOsSysman);
    pSysfsAccess = &pLinuxSysmanImp->getSysfsAccess();
    pFsAccess = &pLinuxSysmanImp->getFsAccess();
    getPciIdPathTag();
}

OsEvents *OsEvents::create(OsSysman *pOsSysman) {
    LinuxEventsImp *pLinuxEventsImp = new LinuxEventsImp(pOsSysman);
    return static_cast<OsEvents *>(pLinuxEventsImp);
}

} // namespace L0