| 1 | #pragma once |
| 2 | |
| 3 | #ifndef __TRACECONFIG_H |
| 4 | #define __TRACECONFIG_H |
| 5 | |
| 6 | #include <Python.h> |
| 7 | |
| 8 | #include <mutex> |
| 9 | #include <string> |
| 10 | #include <unordered_map> |
| 11 | #include <vector> |
| 12 | |
| 13 | class TraceConfig { |
| 14 | public: |
| 15 | TraceConfig(PyObject* list_wrapper, PyObject* base_path, bool profile_all_b) { |
| 16 | // Assumes that each item is a bytes object |
| 17 | owner = list_wrapper; |
| 18 | path_owner = base_path; |
| 19 | Py_IncRef(owner); |
| 20 | Py_IncRef(path_owner); |
| 21 | profile_all = profile_all_b; |
| 22 | auto size = PyList_Size(owner); |
| 23 | items.reserve(size); |
| 24 | for (int i = 0; i < size; i++) { |
| 25 | auto item = PyList_GetItem(owner, i); |
| 26 | auto unic = PyUnicode_AsASCIIString(item); |
| 27 | auto s = PyBytes_AsString(unic); |
| 28 | items.push_back(s); |
| 29 | } |
| 30 | scalene_base_path = PyBytes_AsString(PyUnicode_AsEncodedString(base_path, "utf-8", "strict")); |
| 31 | } |
| 32 | |
| 33 | bool should_trace(char* filename) { |
| 34 | if (!filename) { |
| 35 | // Defensive programming. |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | auto res = _memoize.find(filename); |
| 40 | if (res != _memoize.end()) { |
| 41 | return res->second; |
| 42 | } |
| 43 | // Return false if filename contains paths corresponding to the native |
| 44 | // Python libraries. This is to avoid profiling the Python interpreter |
| 45 | // itself. Also exclude site-packages and any IPython files. |
| 46 | |
| 47 | #if defined(_WIN32) |
| 48 | // If on Windows, use \\ as the path separator. |
| 49 | const auto PATH_SEP = "\\"; |
| 50 | #else |
| 51 | // Assume all others are POSIX. |
| 52 | const auto PATH_SEP = "/"; |
| 53 | #endif |
| 54 | |
| 55 | if (!profile_all) { |
| 56 | auto python_lib = |
| 57 | std::string("lib") + std::string(PATH_SEP) + std::string("python"); |
| 58 | auto scalene_lib = std::string("scalene") + std::string(PATH_SEP) + |
| 59 | std::string("scalene"); |
| 60 | auto anaconda_lib = |
| 61 | std::string("anaconda3") + std::string(PATH_SEP) + std::string("lib"); |
| 62 | |
| 63 | if (strstr(filename, python_lib.c_str()) || |
| 64 | strstr(filename, scalene_lib.c_str()) || |
| 65 | strstr(filename, anaconda_lib.c_str()) || |
| 66 | // strstr(filename, "site-packages") != nullptr || |
| 67 | (strstr(filename, "<") && |
| 68 | (strstr(filename, "<ipython") || strstr(filename, "<frozen")))) { |
| 69 | _memoize.insert( |
| 70 | std::pair<std::string, bool>(std::string(filename), false)); |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (owner != nullptr) { |
| 76 | for (char* traceable : items) { |
| 77 | if (strstr(filename, traceable)) { |
| 78 | _memoize.insert( |
| 79 | std::pair<std::string, bool>(std::string(filename), true)); |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Temporarily change the current working directory to the original program |
| 86 | // path. |
| 87 | char original_cwd_buf[PATH_MAX]; |
| 88 | #ifdef _WIN32 |
| 89 | auto oldcwd = _getcwd(original_cwd_buf, PATH_MAX); |
| 90 | #else |
| 91 | auto oldcwd = getcwd(original_cwd_buf, PATH_MAX); |
| 92 | #endif |
| 93 | chdir(scalene_base_path); |
| 94 | char resolved_path[PATH_MAX]; |
| 95 | |
| 96 | // Check to see if the file we are profiling is in the original path. |
| 97 | bool did_resolve_path = realpath(filename, resolved_path); |
| 98 | bool result = false; |
| 99 | if (did_resolve_path) { |
| 100 | // True if we found this file in the original path. |
| 101 | result = (strstr(resolved_path, scalene_base_path) != nullptr); |
| 102 | } |
| 103 | |
| 104 | // Now change back to the original current working directory. |
| 105 | chdir(oldcwd); |
| 106 | _memoize.insert( |
| 107 | std::pair<std::string, bool>(std::string(filename), result)); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | void print() { |
| 112 | printf("Profile all? %d\nitems {", profile_all); |
| 113 | for (auto c : items) { |
| 114 | printf("\t%s\n", c); |
| 115 | } |
| 116 | printf("}\n"); |
| 117 | } |
| 118 | |
| 119 | static void setInstance(TraceConfig* instance) { |
| 120 | std::lock_guard<decltype(_instanceMutex)> g(_instanceMutex); |
| 121 | delete _instance; |
| 122 | _instance = instance; |
| 123 | } |
| 124 | |
| 125 | static TraceConfig* getInstance() { |
| 126 | std::lock_guard<decltype(_instanceMutex)> g(_instanceMutex); |
| 127 | return _instance; |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | std::vector<char*> items; |
| 132 | char* scalene_base_path; |
| 133 | // This is to keep the object in scope so that |
| 134 | // the data pointers are always valid |
| 135 | PyObject* owner; |
| 136 | PyObject* path_owner; |
| 137 | bool profile_all; |
| 138 | |
| 139 | static std::mutex _instanceMutex; |
| 140 | static TraceConfig* _instance; |
| 141 | static std::unordered_map<std::string, bool> _memoize; |
| 142 | }; |
| 143 | |
| 144 | #endif |