|
38 | 38 | #include <vector> |
39 | 39 | #include <ctime> |
40 | 40 |
|
| 41 | +#if defined(PSP_ENABLE_WASM) || defined(__linux__) |
| 42 | +#include <malloc.h> |
| 43 | +#endif |
| 44 | + |
41 | 45 | #if !defined(WIN32) && !defined(PSP_ENABLE_WASM) |
42 | 46 | #include <sys/resource.h> |
43 | 47 | #endif |
@@ -764,18 +768,28 @@ ServerResources::get_table_deleted_client(const t_id& table_id) { |
764 | 768 |
|
765 | 769 | std::uint32_t |
766 | 770 | ProtoServer::new_session() { |
| 771 | + if (m_cpu_time_start.load().time_since_epoch().count() == 0) { |
| 772 | + m_cpu_time_start = std::chrono::high_resolution_clock::now(); |
| 773 | + } |
| 774 | + |
767 | 775 | return m_client_id++; |
768 | 776 | } |
769 | 777 |
|
770 | 778 | void |
771 | 779 | ProtoServer::close_session(const std::uint32_t client_id) { |
| 780 | + const auto start = std::chrono::high_resolution_clock::now(); |
772 | 781 | m_resources.drop_client(client_id); |
| 782 | + const auto end = std::chrono::high_resolution_clock::now(); |
| 783 | + m_cpu_time += |
| 784 | + std::chrono::duration_cast<std::chrono::milliseconds>(end - start) |
| 785 | + .count(); |
773 | 786 | } |
774 | 787 |
|
775 | 788 | std::vector<ProtoServerResp<std::string>> |
776 | 789 | ProtoServer::handle_request( |
777 | 790 | std::uint32_t client_id, const std::string_view& data |
778 | 791 | ) { |
| 792 | + const auto start = std::chrono::high_resolution_clock::now(); |
779 | 793 | proto::Request req_env; |
780 | 794 | req_env.ParseFromString(data); |
781 | 795 | std::vector<ProtoServerResp<std::string>> serialized_responses; |
@@ -828,11 +842,17 @@ ProtoServer::handle_request( |
828 | 842 | serialized_responses.emplace_back(std::move(str_resp)); |
829 | 843 | } |
830 | 844 |
|
| 845 | + const auto end = std::chrono::high_resolution_clock::now(); |
| 846 | + m_cpu_time += |
| 847 | + std::chrono::duration_cast<std::chrono::milliseconds>(end - start) |
| 848 | + .count(); |
| 849 | + |
831 | 850 | return serialized_responses; |
832 | 851 | } |
833 | 852 |
|
834 | 853 | std::vector<ProtoServerResp<std::string>> |
835 | 854 | ProtoServer::poll() { |
| 855 | + const auto start = std::chrono::high_resolution_clock::now(); |
836 | 856 | std::vector<ProtoServerResp<std::string>> out; |
837 | 857 | try { |
838 | 858 | const auto& responses = _poll(); |
@@ -880,6 +900,11 @@ ProtoServer::poll() { |
880 | 900 | out.emplace_back(std::move(str_resp)); |
881 | 901 | } |
882 | 902 |
|
| 903 | + const auto end = std::chrono::high_resolution_clock::now(); |
| 904 | + m_cpu_time += |
| 905 | + std::chrono::duration_cast<std::chrono::milliseconds>(end - start) |
| 906 | + .count(); |
| 907 | + |
883 | 908 | return out; |
884 | 909 | } |
885 | 910 |
|
@@ -2664,13 +2689,30 @@ ProtoServer::_handle_request(std::uint32_t client_id, Request&& req) { |
2664 | 2689 | proto::Response resp; |
2665 | 2690 | auto* sys_info = resp.mutable_server_system_info_resp(); |
2666 | 2691 | #if defined(PSP_ENABLE_WASM) && !defined(PSP_ENABLE_PYTHON) |
2667 | | - auto heap_size = psp_heap_size(); |
| 2692 | + const auto heap_size = psp_heap_size(); |
2668 | 2693 | sys_info->set_heap_size(heap_size); |
2669 | | -#elif !defined(WIN32) && !defined(PSP_ENABLE_WASM) |
| 2694 | + const auto res = mallinfo(); |
| 2695 | + sys_info->set_used_size(res.uordblks); |
| 2696 | +#elif defined(__linux__) && !defined(PSP_ENABLE_WASM) |
| 2697 | + auto res = mallinfo(); |
| 2698 | + sys_info->set_heap_size(res.usmblks); |
| 2699 | + sys_info->set_used_size(res.uordblks); |
| 2700 | +#elif defined(__APPLE__) && !defined(PSP_ENABLE_WASM) |
2670 | 2701 | rusage out; |
2671 | 2702 | getrusage(RUSAGE_SELF, &out); |
2672 | 2703 | sys_info->set_heap_size(out.ru_maxrss); |
2673 | 2704 | #endif |
| 2705 | + sys_info->set_cpu_time(m_cpu_time); |
| 2706 | + sys_info->set_cpu_time_epoch( |
| 2707 | + std::chrono::duration_cast<std::chrono::milliseconds>( |
| 2708 | + std::chrono::high_resolution_clock::now() |
| 2709 | + - m_cpu_time_start.load() |
| 2710 | + ) |
| 2711 | + .count() |
| 2712 | + ); |
| 2713 | + |
| 2714 | + m_cpu_time_start = std::chrono::high_resolution_clock::now(); |
| 2715 | + m_cpu_time = 0; |
2674 | 2716 | push_resp(std::move(resp)); |
2675 | 2717 | break; |
2676 | 2718 | } |
|
0 commit comments