separate 1 done
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
CXX := g++
|
||||
PYTHON := python3
|
||||
PIP := $(PYTHON) -m pip
|
||||
|
||||
CXXFLAGS := -Wall -Wextra -O2 -std=c++17
|
||||
LDFLAGS := -lrt
|
||||
|
||||
TARGET := lab1
|
||||
SRC := main.cpp
|
||||
LOG_FILE := log.txt
|
||||
XLSX_FILE:= process_log.xlsx
|
||||
|
||||
.PHONY: all run log excel export report deps clean rebuild
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(SRC)
|
||||
$(CXX) $(CXXFLAGS) $(SRC) -o $(TARGET) $(LDFLAGS)
|
||||
|
||||
run: $(TARGET)
|
||||
./$(TARGET)
|
||||
|
||||
log: $(TARGET)
|
||||
./$(TARGET) > $(LOG_FILE)
|
||||
|
||||
excel: log
|
||||
$(PYTHON) export.py --input $(LOG_FILE) --output $(XLSX_FILE)
|
||||
|
||||
deps:
|
||||
$(PIP) install -r req.txt
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET) $(XLSX_FILE)
|
||||
|
||||
rebuild: clean all
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import re
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
from typing import Dict, Iterable, List, Tuple
|
||||
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.chart import BarChart, Reference
|
||||
from openpyxl.styles import Font
|
||||
|
||||
|
||||
LINE_RE = re.compile(
|
||||
r"^(START|END)\s+PID=(\d+)\s+PPID=(\d+)\s+depth=(\d+)\s+range=\[(\d+),(\d+)\]\s+time=([0-9.]+)$"
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProcessLog:
|
||||
pid: int
|
||||
ppid: int
|
||||
start: Decimal
|
||||
start_text: str
|
||||
finish: Decimal | None = None
|
||||
finish_text: str | None = None
|
||||
first_seen_order: int = 0
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert process START/END log to a ptree-like Excel report."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-i",
|
||||
"--input",
|
||||
default="-",
|
||||
help="Input log file path (default: stdin)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
default="process_log.xlsx",
|
||||
help="Output Excel file path (default: process_log.xlsx)",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def read_lines(input_path: str) -> Iterable[str]:
|
||||
if input_path == "-":
|
||||
for line in sys.stdin:
|
||||
yield line.rstrip("\n")
|
||||
return
|
||||
|
||||
with open(input_path, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
yield line.rstrip("\n")
|
||||
|
||||
|
||||
def parse_processes(lines: Iterable[str]) -> Dict[int, ProcessLog]:
|
||||
processes: Dict[int, ProcessLog] = {}
|
||||
order = 0
|
||||
|
||||
for raw in lines:
|
||||
m = LINE_RE.match(raw.strip())
|
||||
if not m:
|
||||
continue
|
||||
|
||||
event, pid_s, ppid_s, _depth_s, _l_s, _r_s, ts_s = m.groups()
|
||||
pid = int(pid_s)
|
||||
ppid = int(ppid_s)
|
||||
ts = Decimal(ts_s)
|
||||
|
||||
if event == "START":
|
||||
processes[pid] = ProcessLog(
|
||||
pid=pid,
|
||||
ppid=ppid,
|
||||
start=ts,
|
||||
start_text=ts_s,
|
||||
finish=None,
|
||||
finish_text=None,
|
||||
first_seen_order=order,
|
||||
)
|
||||
order += 1
|
||||
else:
|
||||
if pid in processes:
|
||||
processes[pid].finish = ts
|
||||
processes[pid].finish_text = ts_s
|
||||
|
||||
return processes
|
||||
|
||||
|
||||
def tree_rows(processes: Dict[int, ProcessLog]) -> List[Tuple[int, ProcessLog]]:
|
||||
complete = {pid: p for pid, p in processes.items() if p.finish is not None}
|
||||
|
||||
children: Dict[int, List[int]] = {}
|
||||
for pid in complete:
|
||||
children[pid] = []
|
||||
for pid, node in complete.items():
|
||||
if node.ppid in complete:
|
||||
children[node.ppid].append(pid)
|
||||
|
||||
for pid in children:
|
||||
children[pid].sort(key=lambda cpid: complete[cpid].start)
|
||||
|
||||
roots = [pid for pid, node in complete.items() if node.ppid not in complete]
|
||||
roots.sort(key=lambda pid: complete[pid].start)
|
||||
|
||||
ordered: List[Tuple[int, ProcessLog]] = []
|
||||
|
||||
def dfs(pid: int, depth: int) -> None:
|
||||
ordered.append((depth, complete[pid]))
|
||||
for ch in children[pid]:
|
||||
dfs(ch, depth + 1)
|
||||
|
||||
for root_pid in roots:
|
||||
dfs(root_pid, 0)
|
||||
|
||||
remaining = [pid for pid in complete if pid not in {p.pid for _, p in ordered}]
|
||||
remaining.sort(key=lambda pid: complete[pid].start)
|
||||
for pid in remaining:
|
||||
dfs(pid, 0)
|
||||
|
||||
return ordered
|
||||
|
||||
|
||||
def fmt6(value: Decimal) -> str:
|
||||
return f"{value.quantize(Decimal('0.000001'))}"
|
||||
|
||||
|
||||
def export_excel(rows: List[Tuple[int, ProcessLog]], output_path: str) -> None:
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "process_logs"
|
||||
|
||||
ws.append(["PPID", "PID", "START", "TIME_FROM_START", "FINISH", "DURATION"])
|
||||
for cell in ws[1]:
|
||||
cell.font = Font(bold=True)
|
||||
|
||||
base_start = min((node.start for _, node in rows), default=Decimal("0"))
|
||||
|
||||
for depth, node in rows:
|
||||
if node.finish is None:
|
||||
continue
|
||||
if node.finish_text is None:
|
||||
continue
|
||||
|
||||
time_from_start = node.start - base_start
|
||||
duration = node.finish - node.start
|
||||
pid_display = f"{' ' * depth}{node.pid}"
|
||||
ws.append(
|
||||
[
|
||||
node.ppid,
|
||||
pid_display,
|
||||
node.start_text,
|
||||
fmt6(time_from_start),
|
||||
node.finish_text,
|
||||
fmt6(duration),
|
||||
]
|
||||
)
|
||||
|
||||
ws.column_dimensions["A"].width = 12
|
||||
ws.column_dimensions["B"].width = 20
|
||||
ws.column_dimensions["C"].width = 18
|
||||
ws.column_dimensions["D"].width = 18
|
||||
ws.column_dimensions["E"].width = 18
|
||||
ws.column_dimensions["F"].width = 14
|
||||
ws.freeze_panes = "A2"
|
||||
|
||||
last_row = ws.max_row
|
||||
if last_row >= 2:
|
||||
chart = BarChart()
|
||||
chart.type = "bar"
|
||||
chart.grouping = "stacked"
|
||||
chart.overlap = 100
|
||||
chart.gapWidth = 150
|
||||
chart.legend.position = "b"
|
||||
chart.width = 15
|
||||
chart.height = 7.5
|
||||
chart.x_axis.scaling.orientation = "maxMin"
|
||||
|
||||
data = Reference(ws, min_col=4, max_col=4, min_row=2, max_row=last_row)
|
||||
duration = Reference(ws, min_col=6, max_col=6, min_row=2, max_row=last_row)
|
||||
chart.add_data(data, titles_from_data=False)
|
||||
chart.add_data(duration, titles_from_data=False)
|
||||
chart.series[0].graphicalProperties.noFill = True
|
||||
|
||||
ws.add_chart(chart, "D21")
|
||||
|
||||
wb.save(output_path)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = parse_args()
|
||||
processes = parse_processes(read_lines(args.input))
|
||||
rows = tree_rows(processes)
|
||||
export_excel(rows, args.output)
|
||||
print(f"Saved to {args.output}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
Before:
|
||||
58004 89472 63849 21489 55079 3458 75571 99360 41392 19102 39020 191 15438 77248 35268 63821 27080 47931 57519 22445
|
||||
|
||||
START PID=22020 PPID=22019 depth=0 range=[0,19999] time=1776915463.198584
|
||||
START PID=22021 PPID=22020 depth=1 range=[0,9999] time=1776915463.198737
|
||||
START PID=22022 PPID=22020 depth=1 range=[10000,19999] time=1776915463.198837
|
||||
START PID=22023 PPID=22021 depth=2 range=[0,4999] time=1776915463.198895
|
||||
START PID=22024 PPID=22021 depth=2 range=[5000,9999] time=1776915463.198963
|
||||
START PID=22025 PPID=22022 depth=2 range=[10000,14999] time=1776915463.199044
|
||||
START PID=22026 PPID=22022 depth=2 range=[15000,19999] time=1776915463.199059
|
||||
START PID=22027 PPID=22023 depth=3 range=[0,2499] time=1776915463.199078
|
||||
START PID=22028 PPID=22024 depth=3 range=[5000,7499] time=1776915463.199143
|
||||
START PID=22029 PPID=22023 depth=3 range=[2500,4999] time=1776915463.199144
|
||||
START PID=22030 PPID=22024 depth=3 range=[7500,9999] time=1776915463.199226
|
||||
START PID=22033 PPID=22025 depth=3 range=[12500,14999] time=1776915463.199313
|
||||
START PID=22031 PPID=22025 depth=3 range=[10000,12499] time=1776915463.199356
|
||||
START PID=22032 PPID=22026 depth=3 range=[15000,17499] time=1776915463.199389
|
||||
END PID=22027 PPID=22023 depth=3 range=[0,2499] time=1776915463.199444
|
||||
END PID=22028 PPID=22024 depth=3 range=[5000,7499] time=1776915463.199544
|
||||
END PID=22029 PPID=22023 depth=3 range=[2500,4999] time=1776915463.199582
|
||||
END PID=22030 PPID=22024 depth=3 range=[7500,9999] time=1776915463.199608
|
||||
END PID=22031 PPID=22025 depth=3 range=[10000,12499] time=1776915463.199723
|
||||
END PID=22033 PPID=22025 depth=3 range=[12500,14999] time=1776915463.199746
|
||||
END PID=22023 PPID=22021 depth=2 range=[0,4999] time=1776915463.199774
|
||||
END PID=22024 PPID=22021 depth=2 range=[5000,9999] time=1776915463.199782
|
||||
START PID=22034 PPID=22026 depth=3 range=[17500,19999] time=1776915463.199843
|
||||
END PID=22025 PPID=22022 depth=2 range=[10000,14999] time=1776915463.199932
|
||||
END PID=22021 PPID=22020 depth=1 range=[0,9999] time=1776915463.199996
|
||||
END PID=22034 PPID=22026 depth=3 range=[17500,19999] time=1776915463.200103
|
||||
END PID=22032 PPID=22026 depth=3 range=[15000,17499] time=1776915463.200222
|
||||
END PID=22026 PPID=22022 depth=2 range=[15000,19999] time=1776915463.200412
|
||||
END PID=22022 PPID=22020 depth=1 range=[10000,19999] time=1776915463.200655
|
||||
END PID=22020 PPID=22019 depth=0 range=[0,19999] time=1776915463.200993
|
||||
|
||||
After:
|
||||
10 11 20 40 40 43 51 56 58 72 74 80 96 96 98 103 108 113 119 137
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kDefaultN = 20000;
|
||||
constexpr int kDefaultMaxDepth = 3;
|
||||
constexpr int kPreviewCount = 20;
|
||||
|
||||
std::string now() {
|
||||
timeval tv{};
|
||||
gettimeofday(&tv, nullptr);
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << tv.tv_sec << "." << std::setfill('0') << std::setw(6) << tv.tv_usec;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void log_start(int l, int r, int depth) {
|
||||
std::cout << "START PID=" << getpid() << " PPID=" << getppid() << " depth=" << depth
|
||||
<< " range=[" << l << "," << r << "] time=" << now() << '\n'
|
||||
<< std::flush;
|
||||
}
|
||||
|
||||
void log_end(int l, int r, int depth) {
|
||||
std::cout << "END PID=" << getpid() << " PPID=" << getppid() << " depth=" << depth
|
||||
<< " range=[" << l << "," << r << "] time=" << now() << '\n'
|
||||
<< std::flush;
|
||||
}
|
||||
|
||||
void merge_range(int* arr, int l, int m, int r) {
|
||||
std::vector<int> temp;
|
||||
temp.reserve(r - l + 1);
|
||||
|
||||
int i = l;
|
||||
int j = m + 1;
|
||||
|
||||
while (i <= m && j <= r) {
|
||||
if (arr[i] <= arr[j]) {
|
||||
temp.push_back(arr[i++]);
|
||||
} else {
|
||||
temp.push_back(arr[j++]);
|
||||
}
|
||||
}
|
||||
|
||||
while (i <= m) {
|
||||
temp.push_back(arr[i++]);
|
||||
}
|
||||
while (j <= r) {
|
||||
temp.push_back(arr[j++]);
|
||||
}
|
||||
|
||||
std::copy(temp.begin(), temp.end(), arr + l);
|
||||
}
|
||||
|
||||
void local_sort(int* arr, int l, int r) {
|
||||
if (l >= r) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int m = l + (r - l) / 2;
|
||||
local_sort(arr, l, m);
|
||||
local_sort(arr, m + 1, r);
|
||||
merge_range(arr, l, m, r);
|
||||
}
|
||||
|
||||
void parallel_sort(int* arr, int l, int r, int depth, int max_depth) {
|
||||
log_start(l, r, depth);
|
||||
|
||||
if (l >= r) {
|
||||
log_end(l, r, depth);
|
||||
return;
|
||||
}
|
||||
|
||||
const int m = l + (r - l) / 2;
|
||||
if (depth >= max_depth) {
|
||||
local_sort(arr, l, r);
|
||||
log_end(l, r, depth);
|
||||
return;
|
||||
}
|
||||
|
||||
pid_t left = fork();
|
||||
if (left == 0) {
|
||||
parallel_sort(arr, l, m, depth + 1, max_depth);
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (left < 0) {
|
||||
std::perror("fork left");
|
||||
local_sort(arr, l, m);
|
||||
}
|
||||
|
||||
pid_t right = fork();
|
||||
if (right == 0) {
|
||||
parallel_sort(arr, m + 1, r, depth + 1, max_depth);
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (right < 0) {
|
||||
std::perror("fork right");
|
||||
local_sort(arr, m + 1, r);
|
||||
}
|
||||
|
||||
int left_status = 0;
|
||||
if (left > 0 && waitpid(left, &left_status, 0) < 0) {
|
||||
std::perror("waitpid left");
|
||||
local_sort(arr, l, m);
|
||||
} else if (left > 0 && (!WIFEXITED(left_status) || WEXITSTATUS(left_status) != 0)) {
|
||||
local_sort(arr, l, m);
|
||||
}
|
||||
|
||||
int right_status = 0;
|
||||
if (right > 0 && waitpid(right, &right_status, 0) < 0) {
|
||||
std::perror("waitpid right");
|
||||
local_sort(arr, m + 1, r);
|
||||
} else if (right > 0 && (!WIFEXITED(right_status) || WEXITSTATUS(right_status) != 0)) {
|
||||
local_sort(arr, m + 1, r);
|
||||
}
|
||||
|
||||
merge_range(arr, l, m, r);
|
||||
log_end(l, r, depth);
|
||||
}
|
||||
|
||||
bool parse_positive_int(const char* value, int& out) {
|
||||
try {
|
||||
size_t consumed = 0;
|
||||
const int parsed = std::stoi(value, &consumed);
|
||||
if (value[consumed] != '\0') {
|
||||
return false;
|
||||
}
|
||||
if (parsed <= 0) {
|
||||
return false;
|
||||
}
|
||||
out = parsed;
|
||||
return true;
|
||||
} catch (const std::invalid_argument&) {
|
||||
return false;
|
||||
} catch (const std::out_of_range&) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int n = kDefaultN;
|
||||
int max_depth = kDefaultMaxDepth;
|
||||
|
||||
if (argc >= 2 && !parse_positive_int(argv[1], n)) {
|
||||
std::cerr << "Invalid array size: " << argv[1] << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (argc >= 3 && !parse_positive_int(argv[2], max_depth)) {
|
||||
std::cerr << "Invalid max depth: " << argv[2] << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const size_t shm_size = static_cast<size_t>(n) * sizeof(int);
|
||||
const int shmid = shmget(IPC_PRIVATE, shm_size, IPC_CREAT | 0600);
|
||||
if (shmid < 0) {
|
||||
std::perror("shmget");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int* arr = static_cast<int*>(shmat(shmid, nullptr, 0));
|
||||
if (arr == reinterpret_cast<void*>(-1)) {
|
||||
std::perror("shmat");
|
||||
shmctl(shmid, IPC_RMID, nullptr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
std::mt19937 rng(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(0, 99999);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
arr[i] = dist(rng);
|
||||
}
|
||||
|
||||
std::cout << "Before:\n";
|
||||
for (int i = 0; i < std::min(n, kPreviewCount); ++i) {
|
||||
std::cout << arr[i] << ' ';
|
||||
}
|
||||
std::cout << "\n\n";
|
||||
|
||||
parallel_sort(arr, 0, n - 1, 0, max_depth);
|
||||
|
||||
std::cout << "\nAfter:\n";
|
||||
for (int i = 0; i < std::min(n, kPreviewCount); ++i) {
|
||||
std::cout << arr[i] << ' ';
|
||||
}
|
||||
std::cout << '\n';
|
||||
|
||||
if (shmdt(arr) < 0) {
|
||||
std::perror("shmdt");
|
||||
}
|
||||
if (shmctl(shmid, IPC_RMID, nullptr) < 0) {
|
||||
std::perror("shmctl IPC_RMID");
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user