rpc_dump.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef BRPC_RPC_DUMP_H
  18. #define BRPC_RPC_DUMP_H
  19. #include <gflags/gflags_declare.h>
  20. #include "butil/iobuf.h" // IOBuf
  21. #include "butil/files/file_path.h" // FilePath
  22. #include "bvar/collector.h"
  23. #include "brpc/rpc_dump.pb.h" // RpcDumpMeta
  24. namespace butil {
  25. class FileEnumerator;
  26. }
  27. namespace brpc {
  28. DECLARE_bool(rpc_dump);
  29. // Randomly take samples of all requests and write into a file in batch in
  30. // a background thread.
  31. // Example:
  32. // SampledRequest* sample = AskToBeSampled();
  33. // If (sample) {
  34. // sample->xxx = yyy;
  35. // sample->request = ...;
  36. // sample->Submit();
  37. // }
  38. //
  39. // In practice, sampled requests are just small fraction of all requests.
  40. // The overhead of sampling should be negligible for overall performance.
  41. class SampledRequest : public bvar::Collected {
  42. public:
  43. butil::IOBuf request;
  44. RpcDumpMeta meta;
  45. // Implement methods of Sampled.
  46. void dump_and_destroy(size_t round) override;
  47. void destroy() override;
  48. bvar::CollectorSpeedLimit* speed_limit() override {
  49. extern bvar::CollectorSpeedLimit g_rpc_dump_sl;
  50. return &g_rpc_dump_sl;
  51. }
  52. };
  53. // If this function returns non-NULL, the caller must fill the returned
  54. // object and submit it for later dumping by calling SubmitSample(). If
  55. // the caller ignores non-NULL return value, the object is leaked.
  56. inline SampledRequest* AskToBeSampled() {
  57. extern bvar::CollectorSpeedLimit g_rpc_dump_sl;
  58. if (!FLAGS_rpc_dump || !bvar::is_collectable(&g_rpc_dump_sl)) {
  59. return NULL;
  60. }
  61. return new (std::nothrow) SampledRequest;
  62. }
  63. // Read samples from dumped files in a directory.
  64. // Example:
  65. // SampleIterator it("./rpc_dump_echo_server");
  66. // for (SampleRequest* req = it->Next(); req != NULL; req = it->Next()) {
  67. // ...
  68. // }
  69. class SampleIterator {
  70. public:
  71. explicit SampleIterator(const butil::StringPiece& dir);
  72. ~SampleIterator();
  73. // Read a sample. Order of samples are not guaranteed to be same with
  74. // the order that they're stored in dumped files.
  75. // Returns the sample which should be deleted by caller. NULL means
  76. // all dumped files are read.
  77. SampledRequest* Next();
  78. private:
  79. // Parse on request from the buf. Set `format_error' to true when
  80. // the buf does not match the format.
  81. static SampledRequest* Pop(butil::IOBuf& buf, bool* format_error);
  82. butil::IOPortal _cur_buf;
  83. int _cur_fd;
  84. butil::FileEnumerator* _enum;
  85. butil::FilePath _dir;
  86. };
  87. } // namespace brpc
  88. #endif // BRPC_RPC_DUMP_H