File: os_events_imp_prelim.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 (237 lines) | stat: -rw-r--r-- 8,895 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
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
/*
 * Copyright (C) 2022 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

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

#include "level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.h"

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

namespace L0 {

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

static bool checkRasEventOccured(Ras *rasHandle) {
    zes_ras_config_t config = {};
    zes_ras_state_t state = {};
    rasHandle->rasGetConfig(&config);
    if (ZE_RESULT_SUCCESS == rasHandle->rasGetState(&state, 0)) {
        uint64_t totalCategoryThreshold = 0;
        for (int i = 0; i < ZES_MAX_RAS_ERROR_CATEGORY_COUNT; i++) {
            totalCategoryThreshold += state.category[i];
            if ((config.detailedThresholds.category[i] > 0) && (state.category[i] > config.detailedThresholds.category[i])) {
                return true;
            }
        }
        if ((config.totalThreshold > 0) && (totalCategoryThreshold > config.totalThreshold)) {
            return true;
        }
    }
    return false;
}

bool LinuxEventsImp::checkRasEvent(zes_event_type_flags_t &pEvent) {
    for (auto rasHandle : pLinuxSysmanImp->getSysmanDeviceImp()->pRasHandleContext->handleList) {
        zes_ras_properties_t properties = {};
        rasHandle->rasGetProperties(&properties);
        if ((registeredEvents & ZES_EVENT_TYPE_FLAG_RAS_CORRECTABLE_ERRORS) && (properties.type == ZES_RAS_ERROR_TYPE_CORRECTABLE)) {
            if (checkRasEventOccured(rasHandle) == true) {
                pEvent |= ZES_EVENT_TYPE_FLAG_RAS_CORRECTABLE_ERRORS;
                return true;
            }
        }
        if ((registeredEvents & ZES_EVENT_TYPE_FLAG_RAS_UNCORRECTABLE_ERRORS) && (properties.type == ZES_RAS_ERROR_TYPE_UNCORRECTABLE)) {
            if (checkRasEventOccured(rasHandle) == true) {
                pEvent |= ZES_EVENT_TYPE_FLAG_RAS_UNCORRECTABLE_ERRORS;
                return true;
            }
        }
    }
    return false;
}

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::checkIfFabricPortStatusChanged(zes_event_type_flags_t &pEvent) {
    uint32_t currentFabricEventStatusVal = 0;
    if (currentFabricEventStatus(currentFabricEventStatusVal) != ZE_RESULT_SUCCESS) {
        return false;
    }
    if (currentFabricEventStatusVal != fabricEventTrackAtRegister) {
        pEvent |= ZES_EVENT_TYPE_FLAG_FABRIC_PORT_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;
        }
    }
    if ((registeredEvents & ZES_EVENT_TYPE_FLAG_RAS_CORRECTABLE_ERRORS) || (registeredEvents & ZES_EVENT_TYPE_FLAG_RAS_UNCORRECTABLE_ERRORS)) {
        if (checkRasEvent(pEvent)) {
            if (pEvent & ZES_EVENT_TYPE_FLAG_RAS_CORRECTABLE_ERRORS) {
                registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_RAS_CORRECTABLE_ERRORS);
            } else {
                registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_RAS_UNCORRECTABLE_ERRORS);
            }
            return true;
        }
    }
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_FABRIC_PORT_HEALTH) {
        if (checkIfFabricPortStatusChanged(pEvent)) {
            registeredEvents &= ~(ZES_EVENT_TYPE_FLAG_FABRIC_PORT_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();
    }
    if (registeredEvents & ZES_EVENT_TYPE_FLAG_FABRIC_PORT_HEALTH) {
        currentFabricEventStatus(fabricEventTrackAtRegister);
    }
    return ZE_RESULT_SUCCESS;
}

ze_result_t LinuxEventsImp::currentFabricEventStatus(uint32_t &val) {
    // When Fabric port status change uevent is generated, then L0 udev rules will create a file:
    // /var/lib/libze_intel_gpu/fabric-<ID_PATH_TAG>
    // For <ID_PATH_TAG>, check comment in LinuxEventsImp::init()
    const std::string fabric = "fabric";
    const std::string fabricEventFile = fabric + "-" + pciIdPathTag;
    const std::string fabricEventFileAbsolutePath = varFs + fabricEventFile;
    return pFsAccess->read(fabricEventFileAbsolutePath, val);
}

zes_mem_health_t LinuxEventsImp::currentMemHealth() {
    std::string memHealth;
    ze_result_t result = pSysfsAccess->read(deviceMemoryHealth, memHealth);
    if (ZE_RESULT_SUCCESS != result) {
        return ZES_MEM_HEALTH_UNKNOWN;
    }

    auto health = i915ToL0MemHealth.find(memHealth);
    if (health != i915ToL0MemHealth.end()) {
        return i915ToL0MemHealth.at(memHealth);
    }
    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