该函数由主线程中的定时器调用
| 46 | |
| 47 | // 该函数由主线程中的定时器调用 |
| 48 | void ServerManager::buildStatus() |
| 49 | { |
| 50 | // 因为在子线程中也会读取 json_ 对象,所以对单例成员变量 |
| 51 | // json_ 进行加锁保护,为了防止主线程被长期阻塞在锁上,所以 |
| 52 | // 采用了尝试加锁方式 |
| 53 | if (lock_.try_lock() == false) |
| 54 | return; |
| 55 | |
| 56 | json_.reset(); |
| 57 | xml_.reset(); |
| 58 | |
| 59 | acl::json_node& json_servers = json_.create_array(); |
| 60 | json_.get_root().add_child("server", json_servers); |
| 61 | |
| 62 | acl::xml_node& xml_servers = xml_.create_node("server"); |
| 63 | xml_.get_root().add_child(xml_servers); |
| 64 | |
| 65 | long long total_conns = 0, total_used = 0, total_qlen = 0; |
| 66 | long long total_max_threads = 0, total_curr_threads = 0; |
| 67 | long long total_busy_threads = 0; |
| 68 | acl::string load_s; |
| 69 | (void) sysload::get_load(&load_s); // 获得当前系统的负载 |
| 70 | |
| 71 | std::vector<ServerConnection*>::const_iterator cit = conns_.begin(); |
| 72 | for (; cit != conns_.end(); ++cit) |
| 73 | { |
| 74 | total_conns += (*cit)->get_conns(); |
| 75 | total_used += (*cit)->get_used(); |
| 76 | total_qlen += (*cit)->get_qlen(); |
| 77 | total_max_threads += (*cit)->get_max_threads(); |
| 78 | total_curr_threads += (*cit)->get_curr_threads(); |
| 79 | total_busy_threads += (*cit)->get_busy_threads(); |
| 80 | |
| 81 | acl::json_node& json_server = json_.create_node(); |
| 82 | json_server.add_number("conns", (*cit)->get_conns()) |
| 83 | .add_number("used", (*cit)->get_used()) |
| 84 | .add_number("pid", (*cit)->get_pid()) |
| 85 | .add_number("max_threads", (*cit)->get_max_threads()) |
| 86 | .add_number("curr_threads", (*cit)->get_curr_threads()) |
| 87 | .add_number("busy_threads", (*cit)->get_busy_threads()) |
| 88 | .add_number("qlen", (*cit)->get_qlen()) |
| 89 | .add_text("type", (*cit)->get_type()); |
| 90 | json_servers.add_child(json_server); |
| 91 | |
| 92 | xml_servers.add_child("proc", true) |
| 93 | .add_child("conns", (long long int) |
| 94 | (*cit)->get_conns()) |
| 95 | .add_child("used", (long long int) (*cit)->get_used()) |
| 96 | .add_child("pid", (long long int) (*cit)->get_pid()) |
| 97 | .add_child("max_threads", (long long int) |
| 98 | (*cit)->get_max_threads()) |
| 99 | .add_child("curr_threads", (long long int) |
| 100 | (*cit)->get_curr_threads()) |
| 101 | .add_child("busy_threads", (long long int) |
| 102 | (*cit)->get_busy_threads()) |
| 103 | .add_child("qlen", (long long int) (*cit)->get_qlen()) |
| 104 | .add_child("type", (*cit)->get_type().c_str()); |
| 105 | } |
no test coverage detected