omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / gpu / nvidia / driver / diag.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note gpu/nvidia/driver/diag.om: the memory diagnosis the driver client adds when an allocation fails. |
| 3 | note Omega form of CUDApp nvrm/nvrm.c meminfo_kb, buddy_free_blocks_ge, fail_alloc_context and |
| 4 | note fail_channel_no_memory (84b524ca). Parsing reuses gpu/nvidia/native/nvrm/counts.om (nc_mem, nc_buddy), |
| 5 | note which must come first in the composition (it carries the om2 line); gpu/nvidia/driver/parts.txt. |
| 6 | note The caller reads /proc/meminfo and /proc/buddyinfo whole and hands the bytes in; nothing here opens a file. |
| 7 | note The C reads lines in 256 and 512 byte pieces and cuts its message at 255 bytes; here a line is a whole |
| 8 | note line and the message is as long as it needs to be. A caller buffer too small for the message is refused |
| 9 | note (drv_amsg and drv_cmsg return 2^64 - 1), never cut short. counts.om walks at most 512 bytes of one line, |
| 10 | note so a longer line is refused here (false) rather than handed to it. |
| 11 | |
| 12 | text drv_t " for | bytes, MemFree | kB, MemAvailable | kB| after | attempts over | ms; MemFree | kB, free 2 MiB blocks |UNKNOWN|" |
| 13 | const drv_line ulong 512 |
| 14 | const drv_none ulong 18446744073709551615 |
| 15 | |
| 16 | note end of the line that starts at s: the index just past its newline, or len f |
| 17 | fn drv_eol f view uchar * s ulong to ulong |
| 18 | i = s |
| 19 | while lt i len f bound 9223372036854775807 |
| 20 | if eq get f i 10 |
| 21 | ret add i 1 |
| 22 | end |
| 23 | i = add i 1 |
| 24 | end |
| 25 | ret i |
| 26 | end |
| 27 | |
| 28 | note meminfo_kb: r[0] gets the kB value of the first line "KEY:", 0 when there is none or its value does not |
| 29 | note parse (the C answer for an unreadable file is also 0; the caller passes empty bytes then). |
| 30 | fn drv_meminfo f view uchar * key view uchar * r edit ulong 1 to bool |
| 31 | st = new ulong 2 0 |
| 32 | put r 0 0 |
| 33 | s = ulong 0 |
| 34 | while lt s len f bound 9223372036854775807 |
| 35 | e = drv_eol f s |
| 36 | if gt sub e s drv_line |
| 37 | ret false |
| 38 | end |
| 39 | if ne nc_mem part f s sub e s key st 0 |
| 40 | put r 0 get st 1 |
| 41 | ret true |
| 42 | end |
| 43 | s = e |
| 44 | end |
| 45 | ret true |
| 46 | end |
| 47 | |
| 48 | note buddy_free_blocks_ge: r[0] gets the free blocks of every order >= order summed over all zone lines. |
| 49 | note The C answer for an unreadable file, ULLONG_MAX, is the caller's to give. |
| 50 | fn drv_buddy f view uchar * order ulong r edit ulong 1 to bool |
| 51 | st = new ulong 2 0 |
| 52 | put st 0 order |
| 53 | s = ulong 0 |
| 54 | while lt s len f bound 9223372036854775807 |
| 55 | e = drv_eol f s |
| 56 | if gt sub e s drv_line |
| 57 | ret false |
| 58 | end |
| 59 | x = nc_buddy part f s sub e s st |
| 60 | s = e |
| 61 | end |
| 62 | put r 0 get st 1 |
| 63 | ret true |
| 64 | end |
| 65 | |
| 66 | note copy piece w of drv_t (pieces end at "|") to o at n; the new end, or drv_none when o is too small. |
| 67 | note Past the last piece nothing is copied. |
| 68 | fn drv_tput o edit uchar * n ulong w ulong to ulong |
| 69 | if eq n drv_none |
| 70 | ret n |
| 71 | end |
| 72 | k = ulong 0 |
| 73 | i = ulong 0 |
| 74 | while and lt k w lt i len drv_t bound 9223372036854775807 |
| 75 | if eq get drv_t i 124 |
| 76 | k = add k 1 |
| 77 | end |
| 78 | i = add i 1 |
| 79 | end |
| 80 | p = n |
| 81 | if ge i len drv_t |
| 82 | ret p |
| 83 | end |
| 84 | while ne get drv_t i 124 bound 9223372036854775807 |
| 85 | if ge p len o |
| 86 | ret drv_none |
| 87 | end |
| 88 | put o p get drv_t i |
| 89 | p = add p 1 |
| 90 | i = add i 1 |
| 91 | end |
| 92 | ret p |
| 93 | end |
| 94 | |
| 95 | note copy the bytes of b to o at n (the C "%s" of the previous error); a NUL in b ends it, as in C |
| 96 | fn drv_sput o edit uchar * n ulong b view uchar * to ulong |
| 97 | if eq n drv_none |
| 98 | ret n |
| 99 | end |
| 100 | p = n |
| 101 | for i ulong len b bound 9223372036854775807 |
| 102 | if eq get b i 0 |
| 103 | break |
| 104 | end |
| 105 | if ge p len o |
| 106 | ret drv_none |
| 107 | end |
| 108 | put o p get b i |
| 109 | p = add p 1 |
| 110 | end |
| 111 | ret p |
| 112 | end |
| 113 | |
| 114 | note "%llu" of v to o at n |
| 115 | fn drv_nput o edit uchar * n ulong v ulong to ulong |
| 116 | if eq n drv_none |
| 117 | ret n |
| 118 | end |
| 119 | d = new uchar 20 0 |
| 120 | k = ulong 0 |
| 121 | x = v |
| 122 | while true bound 20 |
| 123 | put d k uchar add 48 rem x 10 |
| 124 | k = add k 1 |
| 125 | x = div x 10 |
| 126 | if eq x 0 |
| 127 | break |
| 128 | end |
| 129 | end |
| 130 | if gt add n k len o |
| 131 | ret drv_none |
| 132 | end |
| 133 | for j ulong k bound 20 |
| 134 | put o add n j get d sub sub k j 1 |
| 135 | end |
| 136 | ret add n k |
| 137 | end |
| 138 | |
| 139 | note fail_alloc_context: "%s for %llu bytes, MemFree %llu kB, MemAvailable %llu kB" with v = size, MemFree kB, |
| 140 | note MemAvailable kB. Returns the message length in o, or drv_none when o is too small. |
| 141 | fn drv_amsg prev view uchar * v view ulong 3 o edit uchar * to ulong |
| 142 | n = drv_sput o 0 prev |
| 143 | n = drv_tput o n 0 |
| 144 | n = drv_nput o n get v 0 |
| 145 | n = drv_tput o n 1 |
| 146 | n = drv_nput o n get v 1 |
| 147 | n = drv_tput o n 2 |
| 148 | n = drv_nput o n get v 2 |
| 149 | ret drv_tput o n 3 |
| 150 | end |
| 151 | |
| 152 | note fail_channel_no_memory: "%s after %u attempts over %u ms; MemFree %llu kB, free 2 MiB blocks %s" with |
| 153 | note v = attempts, delay ms (both u32), MemFree kB, free blocks of order >= 9 (drv_none prints UNKNOWN). |
| 154 | note The span is (attempts - 1) * delay in 32-bit arithmetic, as the C computes it. |
| 155 | fn drv_cmsg prev view uchar * v view ulong 4 o edit uchar * to ulong |
| 156 | a = and get v 0 4294967295 |
| 157 | w = ulong 0 |
| 158 | if gt a 1 |
| 159 | w = and mul sub a 1 and get v 1 4294967295 4294967295 |
| 160 | end |
| 161 | n = drv_sput o 0 prev |
| 162 | n = drv_tput o n 4 |
| 163 | n = drv_nput o n a |
| 164 | n = drv_tput o n 5 |
| 165 | n = drv_nput o n w |
| 166 | n = drv_tput o n 6 |
| 167 | n = drv_nput o n get v 2 |
| 168 | n = drv_tput o n 7 |
| 169 | if eq get v 3 drv_none |
| 170 | ret drv_tput o n 8 |
| 171 | end |
| 172 | ret drv_nput o n get v 3 |
| 173 | end |