修订版 | 5710f97d5774deca5e27d7d0ff26f61b3f6bee9c (tree) |
---|---|
时间 | 2020-07-15 03:09:19 |
作者 | Alexander Larin |
Commiter | Alexander Larin |
Implement i_process for rp_linux
@@ -74,6 +74,13 @@ | ||
74 | 74 | return mIsRunning.load(); |
75 | 75 | } |
76 | 76 | |
77 | +bool checklib::details::RestrictedProcessImpl::end_process(ProcessStatus status) | |
78 | +{ | |
79 | + // This is a blank implementation, will be changed soon | |
80 | + terminate(); | |
81 | + return true; | |
82 | +} | |
83 | + | |
77 | 84 | // Запуск процесса |
78 | 85 | void checklib::details::RestrictedProcessImpl::start() |
79 | 86 | { |
@@ -305,7 +312,8 @@ | ||
305 | 312 | // Сколько процессорного времени израсходовал процесс |
306 | 313 | int checklib::details::RestrictedProcessImpl::cpu_time() |
307 | 314 | { |
308 | - return mCPUTime.load(); | |
315 | + if (is_running()) return cpu_time_impl(); | |
316 | + return m_cpu_time; | |
309 | 317 | } |
310 | 318 | |
311 | 319 | checklib::Limits checklib::details::RestrictedProcessImpl::getLimits() const |
@@ -387,7 +395,7 @@ | ||
387 | 395 | mErrorPipe.reset(); |
388 | 396 | |
389 | 397 | mPeakMemoryUsage.store(0); |
390 | - mCPUTime.store(0); | |
398 | + m_cpu_time.store(0); | |
391 | 399 | mIsRunning.store(false); |
392 | 400 | mProcessStatus.store(psNotRunning); |
393 | 401 | } |
@@ -428,7 +436,7 @@ | ||
428 | 436 | cminflt >> majflt >> cmajflt >> utime >> stime; |
429 | 437 | |
430 | 438 | int currentCPUTime = (utime + stime) / mTicks * 1000; |
431 | - mCPUTime.store(currentCPUTime); | |
439 | + m_cpu_time.store(currentCPUTime); | |
432 | 440 | if (mLimits.useTimeLimit && mLimits.timeLimit < currentCPUTime) |
433 | 441 | { |
434 | 442 | mProcessStatus.store(psTimeLimitExceeded); |
@@ -533,3 +541,19 @@ | ||
533 | 541 | } |
534 | 542 | return false; |
535 | 543 | } |
544 | + | |
545 | +int checklib::details::RestrictedProcessImpl::cpu_time_impl() const | |
546 | +{ | |
547 | + std::ostringstream name; | |
548 | + name << mChildPid; | |
549 | + std::ifstream is("/proc/" + name.str() + "/stat"); | |
550 | + | |
551 | + // Получение времени работы процесса. Интересуют нас только последние два числа | |
552 | + std::string pid, comm, state, ppid, pgrp, session, tty_nr; | |
553 | + std::string tpgid, flags, minflt, cminflt, majflt, cmajflt; | |
554 | + long long int utime, stime; | |
555 | + is >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr >> tpgid >> flags >> minflt >> | |
556 | + cminflt >> majflt >> cmajflt >> utime >> stime; | |
557 | + | |
558 | + return (utime + stime) / mTicks * 1000; | |
559 | +} | |
\ No newline at end of file |
@@ -2,6 +2,7 @@ | ||
2 | 2 | |
3 | 3 | #include "../rp_types.h" |
4 | 4 | |
5 | +#include "i_process.hpp" | |
5 | 6 | #include <atomic> |
6 | 7 | #include <boost/asio.hpp> |
7 | 8 | #include <boost/signals2.hpp> |
@@ -9,9 +10,7 @@ | ||
9 | 10 | #include <boost/thread/thread.hpp> |
10 | 11 | #include <memory> |
11 | 12 | |
12 | -namespace checklib | |
13 | -{ | |
14 | -namespace details | |
13 | +namespace checklib::details | |
15 | 14 | { |
16 | 15 | |
17 | 16 | class Pipe |
@@ -38,7 +37,7 @@ | ||
38 | 37 | void reset() { p.reset(); } |
39 | 38 | }; |
40 | 39 | |
41 | -class RestrictedProcessImpl | |
40 | +class RestrictedProcessImpl : public checklib::details::IProcess | |
42 | 41 | { |
43 | 42 | public: |
44 | 43 | RestrictedProcessImpl(); |
@@ -53,12 +52,14 @@ | ||
53 | 52 | std::string currentDirectory() const; |
54 | 53 | void setCurrentDirectory(const std::string &directory); |
55 | 54 | |
56 | - bool is_running() const; | |
55 | + bool is_running() const override; | |
56 | + | |
57 | + bool end_process(ProcessStatus status) override; | |
57 | 58 | |
58 | 59 | void start(); |
59 | 60 | void terminate(); |
60 | 61 | void wait(); |
61 | - bool wait(int milliseconds); | |
62 | + bool wait(int milliseconds) override; | |
62 | 63 | |
63 | 64 | // Код возврата. |
64 | 65 | int exit_code() const; |
@@ -67,10 +68,10 @@ | ||
67 | 68 | ProcessStatus processStatus() const; |
68 | 69 | |
69 | 70 | // Пиковое значение потребляемой памяти |
70 | - int peak_memory_usage(); | |
71 | + int peak_memory_usage() override; | |
71 | 72 | |
72 | 73 | // Сколько процессорного времени израсходовал процесс |
73 | - int cpu_time(); | |
74 | + int cpu_time() override; | |
74 | 75 | |
75 | 76 | void reset(); |
76 | 77 |
@@ -118,7 +119,7 @@ | ||
118 | 119 | int mUnchangedTicks; |
119 | 120 | int mOldCPUTime; |
120 | 121 | |
121 | - mutable std::atomic<int> mCPUTime, mPeakMemoryUsage; | |
122 | + mutable std::atomic<int> m_cpu_time, mPeakMemoryUsage; | |
122 | 123 | std::atomic<bool> mIsRunning; |
123 | 124 | |
124 | 125 | Pipe mInputPipe, mOutputPipe, mErrorPipe; |
@@ -137,8 +138,7 @@ | ||
137 | 138 | |
138 | 139 | int peakMemoryUsageS() const; |
139 | 140 | |
140 | - int CPUTimeS() const; | |
141 | + int cpu_time_impl() const; | |
141 | 142 | }; |
142 | 143 | |
143 | -} // namespace details | |
144 | -} // namespace checklib | |
144 | +} // namespace checklib::details |