okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/src/engine/missing.cpp - what a kernel entry point that has no Metal implementation yet does when called. 2// 3// Default: say which one and stop (exit code 86), so a run never continues on outputs nobody wrote. 4// STRATA_METAL_STUBS=log: say it once per entry point and return (outputs untouched) - for listing which entry points a 5// code path reaches, never for answers. 6#include <cstdio> 7#include <cstdlib> 8#include <mutex> 9#include <set> 10#include <string> 11 12namespace strata::metal_engine { 13 14void missing (const char * name ) { 15static const bool log_only = [] { 16const char * v = std::getenv ("STRATA_METAL_STUBS" ); 17return v != nullptr && std::string (v )== "log" ; 18 }(); 19if (!log_only ) { 20 std::fprintf (stderr ,"strata metal: %s has no Metal implementation yet\n" ,name ); 21 std::fflush (stderr ); 22 std::_Exit (86 ); 23 } 24static std::mutex mu ; 25static std::set < std::string > seen ; 26 std::lock_guard < std::mutex > lk (mu ); 27if (seen .insert (name ).second ) std::fprintf (stderr ,"strata metal: STUB %s\n" ,name ); 28} 29 30}// namespace strata::metal_engine 31 32// ---- entry points the shared host loop asks every request, with nothing to do on Metal yet 33#include "strata/kernels/cvec.hpp" 34namespace strata::kernels { 35// no control vector on Metal (--control-vector-scaled is not ported): the empty one, `loaded()` false 36const Cvec & cvec () { 37static const Cvec none {}; 38return none ; 39} 40}// namespace strata::kernels 41 42// an uncaught exception or abort on the Metal build prints where it came from (macOS gives no core dump by default and 43// lldb needs a debugging permission a user's session may not have) 44#include <csignal> 45#include <signal.h> 46#include <execinfo.h> 47#include <exception> 48#include <unistd.h> 49#include <dirent.h> 50#include <sys/resource.h> 51#include <mach/mach.h> 52namespace { 53[[noreturn ]]void metal_terminate () { 54void * fr [64 ]; 55const int n = backtrace (fr ,64 ); 56const char msg []= "strata metal: terminate - the call stack:\n" ; 57 (void ) !write (2 ,msg ,sizeof msg - 1 ); 58backtrace_symbols_fd (fr ,n ,2 ); 59 std::abort (); 60} 61const bool g_terminate_set = (std::set_terminate (metal_terminate ), true); 62// a crash (SIGSEGV / SIGBUS) prints its call stack too: macOS keeps no report for a command-line process by default 63void metal_crash (int sig ,siginfo_t * si ,void * ) { 64void * fr [64 ]; 65const int n = backtrace (fr ,64 ); 66char msg [160 ]; 67const int m = std::snprintf (msg ,sizeof msg ,"strata metal: crashed (signal %d at address %p) - the call stack:\n" ,sig , 68si ?si -> si_addr :nullptr ); 69 (void ) !write (2 ,msg , (size_t )m ); 70backtrace_symbols_fd (fr ,n ,2 ); 71 {// what the process held when it crashed (a crash inside the system's Metal compiler has no other clue): descriptors against the limit, memory 72struct rlimit rl {}; 73getrlimit (RLIMIT_NOFILE ,& rl ); 74int fds = 0 ; 75if (DIR * d = opendir ("/dev/fd" )) { 76while (readdir (d )!= nullptr )++ fds ; 77closedir (d ); 78 } 79task_vm_info_data_t ti ; 80mach_msg_type_number_t cnt = TASK_VM_INFO_COUNT ; 81const bool ok = task_info (mach_task_self (),TASK_VM_INFO , (task_info_t )& ti ,& cnt )== KERN_SUCCESS ; 82char m2 [256 ]; 83const int k = std::snprintf (m2 ,sizeof m2 ,"strata metal: open descriptors %d (limit %llu / %llu), virtual %.1f GB, footprint %.2f GB, resident %.2f GB\n" ,fds , 84 (unsigned long long )rl .rlim_cur , (unsigned long long )rl .rlim_max ,ok ?ti .virtual_size /1e9 :-1.0 , 85ok ?ti .phys_footprint /1e9 :-1.0 ,ok ?ti .resident_size /1e9 :-1.0 ); 86 (void ) !write (2 ,m2 , (size_t )k ); 87 } 88signal (sig ,SIG_DFL ); 89raise (sig ); 90} 91bool set_crash () { 92struct sigaction sa {}; 93sa .sa_sigaction = metal_crash ; 94sa .sa_flags = SA_SIGINFO ; 95sigaction (SIGSEGV ,& sa ,nullptr ); 96sigaction (SIGBUS ,& sa ,nullptr ); 97return true; 98} 99const bool g_crash_set = set_crash (); 100}// namespace