File: driver_discovery_lin.cpp

package info (click to toggle)
level-zero 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,020 kB
  • sloc: cpp: 132,430; ansic: 16,654; python: 10,040; makefile: 4
file content (179 lines) | stat: -rw-r--r-- 5,706 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
/*
 *
 * Copyright (C) 2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "source/loader/driver_discovery.h"

#include "source/inc/ze_util.h"
#include <iostream>
#include <sstream>
#include <string>

#include <dlfcn.h>
#include <unistd.h>
#include <dirent.h>

#include <vector>
#include <fstream>
#include <sys/stat.h>
// Helper to split a colon-separated path string
static std::vector<std::string> splitPaths(const std::string& paths) {
  std::vector<std::string> result;
  std::stringstream ss(paths);
  std::string item;
  while (std::getline(ss, item, ':')) {
    if (!item.empty()) result.push_back(item);
  }
  return result;
}

// Helper to check if a file exists and is readable
static bool fileExistsReadable(const std::string& path) {
  struct stat sb;
  return (stat(path.c_str(), &sb) == 0) && (access(path.c_str(), R_OK) == 0);
}

// Helper to get all library search paths from LD_LIBRARY_PATH, standard locations, and /etc/ld.so.conf
static std::vector<std::string> getLibrarySearchPaths() {
  std::vector<std::string> paths;
  // LD_LIBRARY_PATH
  const char* ldLibPath = getenv("LD_LIBRARY_PATH");
  if (ldLibPath) {
    auto split = splitPaths(ldLibPath);
    paths.insert(paths.end(), split.begin(), split.end());
  }
  // Standard locations - Common across all Linux distributions
  paths.push_back("/lib");
  paths.push_back("/usr/lib");
  paths.push_back("/usr/local/lib");

  // Multi-arch paths for Ubuntu/Debian
  paths.push_back("/lib/x86_64-linux-gnu");
  paths.push_back("/usr/lib/x86_64-linux-gnu");
  paths.push_back("/lib/i386-linux-gnu");
  paths.push_back("/usr/lib/i386-linux-gnu");
  paths.push_back("/lib/aarch64-linux-gnu");
  paths.push_back("/usr/lib/aarch64-linux-gnu");

  // 64-bit library paths for RHEL/CentOS/Fedora
  paths.push_back("/lib64");
  paths.push_back("/usr/lib64");
  paths.push_back("/usr/local/lib64");

  // Flatpak/Snap paths for containerized applications
  paths.push_back("/var/lib/flatpak/runtime");
  paths.push_back("/snap/core/current/lib");
  paths.push_back("/snap/core/current/usr/lib");

  // /etc/ld.so.conf and included files
  std::ifstream ldSoConf("/etc/ld.so.conf");
  if (ldSoConf) {
    std::string line;
    while (std::getline(ldSoConf, line)) {
      if (line.empty()) continue;
      if (line.find("include ") == 0) {
        std::string pattern = line.substr(8);
        // Simple glob: /etc/ld.so.conf.d/*.conf
        std::string dir = pattern.substr(0, pattern.find_last_of('/'));
        std::string ext = pattern.substr(pattern.find_last_of('.'));
        DIR* d = opendir(dir.c_str());
        if (d) {
          struct dirent* ent;
          while ((ent = readdir(d)) != nullptr) {
            std::string fname = ent->d_name;
            if (fname.size() > ext.size() && fname.substr(fname.size()-ext.size()) == ext) {
              std::ifstream incFile(dir + "/" + fname);
              std::string incLine;
              while (std::getline(incFile, incLine)) {
                if (!incLine.empty() && incLine[0] != '#')
                  paths.push_back(incLine);
              }
            }
          }
          closedir(d);
        }
      } else if (line[0] != '#') {
        paths.push_back(line);
      }
    }
  }
  return paths;
}

// Main function: search for a library file in all known library paths
static bool libraryExistsInSearchPaths(const std::string& filename) {
  auto paths = getLibrarySearchPaths();
  for (const auto& dir : paths) {
    std::string fullPath = dir + "/" + filename;
    if (fileExistsReadable(fullPath)) {
      return true;
    }
  }
  return false;
}

namespace loader {

static const char *knownDriverNames[] = {
    MAKE_LIBRARY_NAME("ze_intel_gpu", "1"),
    MAKE_LIBRARY_NAME("ze_intel_gpu_legacy1", "1"),
    MAKE_LIBRARY_NAME("ze_intel_vpu", "1"),
    MAKE_LIBRARY_NAME("ze_intel_npu", "1"),
};

std::vector<DriverLibraryPath> discoverEnabledDrivers() {
  std::vector<DriverLibraryPath> enabledDrivers;
  const char *altDrivers = nullptr;

  // ZE_ENABLE_ALT_DRIVERS is for development/debug only
  altDrivers = getenv("ZE_ENABLE_ALT_DRIVERS");
  if (altDrivers == nullptr) {
    // Standard drivers - not custom
    for (auto path : knownDriverNames) {
      if (libraryExistsInSearchPaths(path)) {
        // Extract the base library name for robust driver type detection
        // path is like "libze_intel_gpu.so.1"
        std::string libName = path;

        // Remove "lib" prefix if present
        if (libName.compare(0, 3, "lib") == 0) {
          libName = libName.substr(3);
        }

        // Remove file extension
        size_t dotPos = libName.find('.');
        if (dotPos != std::string::npos) {
          libName = libName.substr(0, dotPos);
        }

        // Now match against the core driver name (e.g., "ze_intel_gpu", "ze_intel_npu")
        // Check for exact matches or word boundaries to avoid substring false positives
        zel_driver_type_t driverType = ZEL_DRIVER_TYPE_FORCE_UINT32;

        // Check more specific patterns first to avoid partial matches
        if (libName == "ze_intel_gpu" || libName == "ze_intel_gpu_legacy1") {
          driverType = ZEL_DRIVER_TYPE_GPU;
        } else if (libName == "ze_intel_vpu" || libName == "ze_intel_npu") {
          driverType = ZEL_DRIVER_TYPE_NPU;
        }

        enabledDrivers.emplace_back(path, false, driverType);
      }
    }
  } else {
    // Alternative drivers from environment variable - these are custom
    std::stringstream ss(altDrivers);
    while (ss.good()) {
      std::string substr;
      getline(ss, substr, ',');
      enabledDrivers.emplace_back(substr, true);
    }
  }
  return enabledDrivers;
}

} // namespace loader