Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
161 lines (145 loc) · 5.35 KB
/
Copy pathmain.cpp
File metadata and controls
161 lines (145 loc) · 5.35 KB
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
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "application.h"
#include "composer.h"
#include "vterm_headless.h"
#ifdef SHITTY_HEAP_PROFILE
#include "heap_profile.h"
#endif
#include <std/ios/in_fd.h>
#include <std/ios/sys.h>
#include <std/lib/buffer.h>
#include <std/lib/vector.h>
#include <std/mem/obj_pool.h>
#include <std/str/builder.h>
#include <std/str/view.h>
#include <std/sys/fd.h>
#include <std/sys/fs.h>
#include <std/sys/throw.h>
#include <chrono>
#include <cstring>
#include <exception>
#include <stdexcept>
#include <string>
#include <fcntl.h>
using namespace stl;
namespace {
constexpr size_t perfFeedChunk = 8 * 1024;
struct PerfFile {
size_t pathOffset;
};
void writeRepeated(ZeroCopyOutput& output, u8 byte, size_t count) {
if (count == 0) {
return;
}
u8* const bytes = static_cast<u8*>(output.imbue(count).ptr);
memset(bytes, byte, count);
output.commit(count);
}
void writeTenths(ZeroCopyOutput& output, double value) {
const u64 tenths = (u64)(value * 10 + 0.5);
output << tenths / 10 << StringView(u8".") << tenths % 10;
}
void showPerfProgress(size_t done, size_t total, size_t bytes, std::chrono::steady_clock::time_point started) {
constexpr size_t width = 40;
const size_t filled = total == 0 ? width : done * width / total;
const double elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - started).count();
const double mib = bytes / (1024.0 * 1024.0);
const double mibPerSecond = elapsed > 0 ? mib / elapsed : 0;
OutBuf output(stderrStream());
output << StringView(u8"\r[");
writeRepeated(output, u8'#', filled);
writeRepeated(output, u8' ', width - filled);
output << StringView(u8"] ") << (u64)(done) << StringView(u8"/") << (u64)(total) << StringView(u8" ");
writeTenths(output, mib);
output << StringView(u8" MiB, ");
writeTenths(output, mibPerSecond);
output << StringView(u8" MiB/s") << flsH;
}
int runPerf(int argc, char* argv[]) {
if (argc < 3) {
throw std::invalid_argument("usage: st perf DIRECTORY...");
}
Buffer paths;
Vector<PerfFile> files;
for (int index = 2; index < argc; ++index) {
const StringView directory(argv[index]);
listDir(directory, [&](const TPathInfo& entry) {
if (!entry.isDir) {
const PerfFile file{.pathOffset = paths.used()};
paths.append(directory.data(), directory.length());
if (directory.empty() || directory.back() != '/') {
const u8 slash = '/';
paths.append(&slash, 1);
}
paths.append(entry.item.data(), entry.item.length());
const u8 zero = 0;
paths.append(&zero, 1);
files.pushBack(file);
}
});
}
ObjPool::Ref pool = ObjPool::fromMemory();
Composer& composer = *pool->make<Composer>(pool.mutPtr());
VtermHeadless* vterm = VtermHeadless::create(composer);
Buffer data;
size_t bytes = 0;
const auto started = std::chrono::steady_clock::now();
showPerfProgress(0, files.length(), bytes, started);
for (size_t index = 0; index < files.length(); ++index) {
const char* path = (const char*)paths.data() + files[index].pathOffset;
const int rawFd = open(path, O_RDONLY);
if (rawFd < 0) {
Errno().raise(StringBuilder() << StringView(u8"cannot open ") << StringView(path));
}
ScopedFD fd(rawFd);
data.reset();
FDInput(fd).readAll(data);
const u8* input = (const u8*)data.data();
size_t remaining = data.used();
while (remaining != 0) {
const size_t length = remaining < perfFeedChunk ? remaining : perfFeedChunk;
vterm->feed(input, length);
input += length;
remaining -= length;
}
bytes += data.used();
if ((index + 1) % 256 == 0 || index + 1 == files.length()) {
showPerfProgress(index + 1, files.length(), bytes, started);
}
}
sysE << endL;
#ifdef SHITTY_HEAP_PROFILE
dumpHeapProfile();
#endif
return 0;
}
}
int main(int argc, char* argv[]) {
int status = 1;
try {
#ifdef SHITTY_HEAP_PROFILE
initializeHeapProfile();
#endif
if (argc > 1 && std::string(argv[1]) == "perf") {
status = runPerf(argc, argv);
} else {
ObjPool::Ref pool = ObjPool::fromMemory();
Composer& composer = *pool->make<Composer>(pool.mutPtr());
composer.application = Application::create(composer);
status = composer.application->run(argc, argv);
#ifdef SHITTY_HEAP_PROFILE
dumpHeapProfile();
#endif
}
} catch (Exception& error) {
const StringView message = error.description();
sysE << StringView(u8"Error: ") << message << endL;
} catch (const std::exception& error) {
sysE << StringView(u8"Error: ") << StringView(error.what()) << endL;
}
return status;
}