|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "butil/time.h" |
| 19 | +#include "butil/logging.h" |
| 20 | +#include "brpc/controller.h" // Controller |
| 21 | +#include "brpc/closure_guard.h" // ClosureGuard |
| 22 | +#include "brpc/builtin/memory_service.h" |
| 23 | +#include "brpc/details/tcmalloc_extension.h" |
| 24 | + |
| 25 | +namespace brpc { |
| 26 | + |
| 27 | +DEFINE_int32(max_tc_stats_buf_len, 32 * 1024, "max length of TCMalloc stats"); |
| 28 | +BRPC_VALIDATE_GFLAG(max_tc_stats_buf_len, PositiveInteger); |
| 29 | + |
| 30 | +static inline void get_tcmalloc_num_prop(MallocExtension* malloc_ext, |
| 31 | + const char* prop_name, |
| 32 | + butil::IOBufBuilder& os) { |
| 33 | + size_t value; |
| 34 | + if (malloc_ext->GetNumericProperty(prop_name, &value)) { |
| 35 | + os << prop_name << ": " << value << "\n"; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +static void get_tcmalloc_memory_info(butil::IOBuf& out) { |
| 40 | + MallocExtension* malloc_ext = MallocExtension::instance(); |
| 41 | + butil::IOBufBuilder os; |
| 42 | + os << "------------------------------------------------\n"; |
| 43 | + get_tcmalloc_num_prop(malloc_ext, "generic.total_physical_bytes", os); |
| 44 | + get_tcmalloc_num_prop(malloc_ext, "generic.current_allocated_bytes", os); |
| 45 | + get_tcmalloc_num_prop(malloc_ext, "generic.heap_size", os); |
| 46 | + get_tcmalloc_num_prop(malloc_ext, "tcmalloc.current_total_thread_cache_bytes", os); |
| 47 | + get_tcmalloc_num_prop(malloc_ext, "tcmalloc.central_cache_free_bytes", os); |
| 48 | + get_tcmalloc_num_prop(malloc_ext, "tcmalloc.transfer_cache_free_bytes", os); |
| 49 | + get_tcmalloc_num_prop(malloc_ext, "tcmalloc.thread_cache_free_bytes", os); |
| 50 | + get_tcmalloc_num_prop(malloc_ext, "tcmalloc.pageheap_free_bytes", os); |
| 51 | + get_tcmalloc_num_prop(malloc_ext, "tcmalloc.pageheap_unmapped_bytes", os); |
| 52 | + |
| 53 | + int32_t len = FLAGS_max_tc_stats_buf_len; |
| 54 | + std::unique_ptr<char[]> buf(new char[len]); |
| 55 | + malloc_ext->GetStats(buf.get(), len); |
| 56 | + os << buf.get(); |
| 57 | + |
| 58 | + os.move_to(out); |
| 59 | +} |
| 60 | + |
| 61 | +void MemoryService::default_method(::google::protobuf::RpcController* cntl_base, |
| 62 | + const ::brpc::MemoryRequest*, |
| 63 | + ::brpc::MemoryResponse*, |
| 64 | + ::google::protobuf::Closure* done) { |
| 65 | + ClosureGuard done_guard(done); |
| 66 | + auto cntl = static_cast<Controller*>(cntl_base); |
| 67 | + cntl->http_response().set_content_type("text/plain"); |
| 68 | + butil::IOBuf& resp = cntl->response_attachment(); |
| 69 | + |
| 70 | + if (IsTCMallocEnabled()) { |
| 71 | + butil::IOBufBuilder os; |
| 72 | + get_tcmalloc_memory_info(resp); |
| 73 | + } else { |
| 74 | + resp.append("tcmalloc is not enabled"); |
| 75 | + cntl->http_response().set_status_code(HTTP_STATUS_FORBIDDEN); |
| 76 | + return; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace brpc |
0 commit comments