Skip to content

Commit e3f6160

Browse files
authored
Merge pull request #2338 from mazunki/feat-utils-fmt
Introduce `util::format`
2 parents 0d7c006 + ae03e51 commit e3f6160

File tree

3 files changed

+198
-26
lines changed

3 files changed

+198
-26
lines changed

api/util/errno.hpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#ifndef UTIL_ERRNO_HPP
2+
#define UTIL_ERRNO_HPP
3+
4+
#include <string_view>
5+
#include <errno.h>
6+
7+
namespace util::format {
8+
9+
constexpr std::string_view errno_name(int e) noexcept {
10+
switch (e) { // list generated from musl's errno.h
11+
case 0: return "OK";
12+
case EPERM: return "EPERM";
13+
case ENOENT: return "ENOENT";
14+
case ESRCH: return "ESRCH";
15+
case EINTR: return "EINTR";
16+
case EIO: return "EIO";
17+
case ENXIO: return "ENXIO";
18+
case E2BIG: return "E2BIG";
19+
case ENOEXEC: return "ENOEXEC";
20+
case EBADF: return "EBADF";
21+
case ECHILD: return "ECHILD";
22+
// case EAGAIN: return "EAGAIN"; // EWOULDBLOCK
23+
case ENOMEM: return "ENOMEM";
24+
case EACCES: return "EACCES";
25+
case EFAULT: return "EFAULT";
26+
case ENOTBLK: return "ENOTBLK";
27+
case EBUSY: return "EBUSY";
28+
case EEXIST: return "EEXIST";
29+
case EXDEV: return "EXDEV";
30+
case ENODEV: return "ENODEV";
31+
case ENOTDIR: return "ENOTDIR";
32+
case EISDIR: return "EISDIR";
33+
case EINVAL: return "EINVAL";
34+
case ENFILE: return "ENFILE";
35+
case EMFILE: return "EMFILE";
36+
case ENOTTY: return "ENOTTY";
37+
case ETXTBSY: return "ETXTBSY";
38+
case EFBIG: return "EFBIG";
39+
case ENOSPC: return "ENOSPC";
40+
case ESPIPE: return "ESPIPE";
41+
case EROFS: return "EROFS";
42+
case EMLINK: return "EMLINK";
43+
case EPIPE: return "EPIPE";
44+
case EDOM: return "EDOM";
45+
case ERANGE: return "ERANGE";
46+
case EDEADLK: return "EDEADLK"; // == EDEADLOCK
47+
case ENAMETOOLONG: return "ENAMETOOLONG";
48+
case ENOLCK: return "ENOLCK";
49+
case ENOSYS: return "ENOSYS";
50+
case ENOTEMPTY: return "ENOTEMPTY";
51+
case ELOOP: return "ELOOP";
52+
case EWOULDBLOCK: return "EWOULDBLOCK"; // == EAGAIN
53+
case ENOMSG: return "ENOMSG";
54+
case EIDRM: return "EIDRM";
55+
case ECHRNG: return "ECHRNG";
56+
case EL2NSYNC: return "EL2NSYNC";
57+
case EL3HLT: return "EL3HLT";
58+
case EL3RST: return "EL3RST";
59+
case ELNRNG: return "ELNRNG";
60+
case EUNATCH: return "EUNATCH";
61+
case ENOCSI: return "ENOCSI";
62+
case EL2HLT: return "EL2HLT";
63+
case EBADE: return "EBADE";
64+
case EBADR: return "EBADR";
65+
case EXFULL: return "EXFULL";
66+
case ENOANO: return "ENOANO";
67+
case EBADRQC: return "EBADRQC";
68+
case EBADSLT: return "EBADSLT";
69+
// case EDEADLOCK: return "EDEADLOCK"; // EDEADLK
70+
case EBFONT: return "EBFONT";
71+
case ENOSTR: return "ENOSTR";
72+
case ENODATA: return "ENODATA";
73+
case ETIME: return "ETIME";
74+
case ENOSR: return "ENOSR";
75+
case ENONET: return "ENONET";
76+
case ENOPKG: return "ENOPKG";
77+
case EREMOTE: return "EREMOTE";
78+
case ENOLINK: return "ENOLINK";
79+
case EADV: return "EADV";
80+
case ESRMNT: return "ESRMNT";
81+
case ECOMM: return "ECOMM";
82+
case EPROTO: return "EPROTO";
83+
case EMULTIHOP: return "EMULTIHOP";
84+
case EDOTDOT: return "EDOTDOT";
85+
case EBADMSG: return "EBADMSG";
86+
case EOVERFLOW: return "EOVERFLOW";
87+
case ENOTUNIQ: return "ENOTUNIQ";
88+
case EBADFD: return "EBADFD";
89+
case EREMCHG: return "EREMCHG";
90+
case ELIBACC: return "ELIBACC";
91+
case ELIBBAD: return "ELIBBAD";
92+
case ELIBSCN: return "ELIBSCN";
93+
case ELIBMAX: return "ELIBMAX";
94+
case ELIBEXEC: return "ELIBEXEC";
95+
case EILSEQ: return "EILSEQ";
96+
case ERESTART: return "ERESTART";
97+
case ESTRPIPE: return "ESTRPIPE";
98+
case EUSERS: return "EUSERS";
99+
case ENOTSOCK: return "ENOTSOCK";
100+
case EDESTADDRREQ: return "EDESTADDRREQ";
101+
case EMSGSIZE: return "EMSGSIZE";
102+
case EPROTOTYPE: return "EPROTOTYPE";
103+
case ENOPROTOOPT: return "ENOPROTOOPT";
104+
case EPROTONOSUPPORT: return "EPROTONOSUPPORT";
105+
case ESOCKTNOSUPPORT: return "ESOCKTNOSUPPORT";
106+
// case EOPNOTSUPP: return "EOPNOTSUPP"; // ENOTSUP
107+
case ENOTSUP: return "ENOTSUP";
108+
case EPFNOSUPPORT: return "EPFNOSUPPORT";
109+
case EAFNOSUPPORT: return "EAFNOSUPPORT";
110+
case EADDRINUSE: return "EADDRINUSE";
111+
case EADDRNOTAVAIL: return "EADDRNOTAVAIL";
112+
case ENETDOWN: return "ENETDOWN";
113+
case ENETUNREACH: return "ENETUNREACH";
114+
case ENETRESET: return "ENETRESET";
115+
case ECONNABORTED: return "ECONNABORTED";
116+
case ECONNRESET: return "ECONNRESET";
117+
case ENOBUFS: return "ENOBUFS";
118+
case EISCONN: return "EISCONN";
119+
case ENOTCONN: return "ENOTCONN";
120+
case ESHUTDOWN: return "ESHUTDOWN";
121+
case ETOOMANYREFS: return "ETOOMANYREFS";
122+
case ETIMEDOUT: return "ETIMEDOUT";
123+
case ECONNREFUSED: return "ECONNREFUSED";
124+
case EHOSTDOWN: return "EHOSTDOWN";
125+
case EHOSTUNREACH: return "EHOSTUNREACH";
126+
case EALREADY: return "EALREADY";
127+
case EINPROGRESS: return "EINPROGRESS";
128+
case ESTALE: return "ESTALE";
129+
case EUCLEAN: return "EUCLEAN";
130+
case ENOTNAM: return "ENOTNAM";
131+
case ENAVAIL: return "ENAVAIL";
132+
case EISNAM: return "EISNAM";
133+
case EREMOTEIO: return "EREMOTEIO";
134+
case EDQUOT: return "EDQUOT";
135+
case ENOMEDIUM: return "ENOMEDIUM";
136+
case EMEDIUMTYPE: return "EMEDIUMTYPE";
137+
case ECANCELED: return "ECANCELED";
138+
case ENOKEY: return "ENOKEY";
139+
case EKEYEXPIRED: return "EKEYEXPIRED";
140+
case EKEYREVOKED: return "EKEYREVOKED";
141+
case EKEYREJECTED: return "EKEYREJECTED";
142+
case EOWNERDEAD: return "EOWNERDEAD";
143+
case ENOTRECOVERABLE: return "ENOTRECOVERABLE";
144+
case ERFKILL: return "ERFKILL";
145+
case EHWPOISON: return "EHWPOISON";
146+
default: return "ERRNO_UNKNOWN";
147+
}
148+
149+
//unreachable(); // TODO: C++23
150+
return "";
151+
}
152+
153+
inline const char* errno_cstr(int e) noexcept {
154+
return errno_name(e).data();
155+
}
156+
157+
} // namespace util
158+
159+
#endif // UTIL_ERRNO_HPP
160+

api/util/pretty.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef UTIL_PRETTY_HPP
2+
#define UTIL_PRETTY_HPP
3+
4+
#include <format>
5+
#include <string>
6+
7+
namespace util::format {
8+
inline std::string to_human_size(std::size_t bytes) {
9+
constexpr std::string_view size_suffixes[] = {
10+
"B", "KiB", "MiB", "GiB", "TiB", "PiB"
11+
};
12+
double value = static_cast<double>(bytes);
13+
std::size_t exponent = 0;
14+
15+
while (value >= 1024.0 && exponent + 1 < std::size(size_suffixes)) {
16+
value /= 1024.0;
17+
exponent++;
18+
}
19+
20+
if (exponent == 0)
21+
return std::format("{} {}", static_cast<std::uint64_t>(value), size_suffixes[exponent]);
22+
else
23+
return std::format("{:.2f} {}", value, size_suffixes[exponent]);
24+
}
25+
26+
inline std::string with_thousands_sep(uint64_t value, char sep = '_', char every = 3) {
27+
std::string s = std::to_string(value);
28+
for (int i = s.size() - every; i > 0; i -= every)
29+
s.insert(i, 1, sep);
30+
return s;
31+
}
32+
} // namespace util
33+
34+
#endif // UTIL_PRETTY_HPP
35+

src/kernel/profile.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <kernel/elf.hpp>
2323
#include <os.hpp>
2424
#include <util/fixed_vector.hpp>
25+
#include <util/pretty.hpp>
2526
#include <unordered_map>
2627
#include <cassert>
2728
#include <algorithm>
@@ -203,33 +204,9 @@ void StackSampler::set_mask(bool mask)
203204
get().discard = mask;
204205
}
205206

206-
207-
inline std::string to_human_size(std::uint64_t bytes) {
208-
constexpr std::string_view size_suffixes[] = {
209-
"B", "KiB", "MiB", "GiB", "TiB", "PiB"
210-
};
211-
double value = static_cast<double>(bytes);
212-
std::size_t exponent = 0;
213-
214-
while (value >= 1024.0 && exponent + 1 < std::size(size_suffixes)) {
215-
value /= 1024.0;
216-
exponent++;
217-
}
218-
219-
if (exponent == 0)
220-
return std::format("{} {}", static_cast<std::uint64_t>(value), size_suffixes[exponent]);
221-
else
222-
return std::format("{:.2f} {}", value, size_suffixes[exponent]);
223-
}
224-
225-
inline std::string with_thousands_sep(uint64_t value, char sep = '_', char every = 3) {
226-
std::string s = std::to_string(value);
227-
for (int i = s.size() - every; i > 0; i -= every)
228-
s.insert(i, 1, sep);
229-
return s;
230-
}
231-
232207
std::string HeapDiag::to_string() {
208+
using namespace util::format;
209+
233210
// TODO: check if heap should be 64 bit instead
234211
static intptr_t last_size = 0;
235212

0 commit comments

Comments
 (0)