table.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "table.h"
  2. //BaseTable::BaseTable(ebpf::BPF *bpf_instance, const std::string &tb_name)
  3. //: bpf(bpf_instance), tb_name(tb_name){}
  4. void BaseTable::printName() {
  5. std::cout << "Table name: " << tb_name << std::endl;
  6. }
  7. std::string cb_fn;
  8. void PerfEventArrayTable::callbackfn(void *cookie, void *data, int data_size) {
  9. Php::Value phpData((const char *) data, data_size);
  10. Php::call(cb_fn.c_str(), nullptr, phpData, data_size);
  11. }
  12. void PerfEventArrayTable::php_open_perf_buffer(Php::Parameters &params) {
  13. cb_fn = params[0].stringValue();
  14. auto res = this->bpf->open_perf_buffer(this->tb_name, &PerfEventArrayTable::callbackfn);
  15. if (!res.ok()) {
  16. throw Php::Exception("open_perf_buffer error:" + res.msg());
  17. }
  18. }
  19. int PerfEventArrayTable::perf_buffer_poll(int timeout_ms) {
  20. return bpf->poll_perf_buffer(this->tb_name, timeout_ms); // perf_reader_event_read
  21. }
  22. Php::Value ArrayTable::php_get_value(Php::Parameters &param) {
  23. auto index = param[0].numericValue();
  24. // todo val type
  25. uint64_t val;
  26. auto table = bpf->get_array_table<uint64_t>(tb_name);
  27. auto res = table.get_value(index, val);
  28. if (!res.ok()) {
  29. throw Php::Exception("Get value error in" + std::string(tb_name));
  30. }
  31. Php::Value phpValue = static_cast<int64_t>(val);
  32. return phpValue;
  33. }
  34. Php::Value StackTraceTable::php_get_values(Php::Parameters &param) {
  35. auto stack_id = param[0].numericValue();
  36. int pid = -1;
  37. if (param.size() > 1) {
  38. auto param_pid = param[1].numericValue();
  39. if (param_pid >= 0) pid = param_pid;
  40. }
  41. auto table = bpf->get_stack_table(tb_name);
  42. auto symbols = table.get_stack_symbol((int) stack_id, (int) pid);
  43. std::vector<std::string> result;
  44. for (const auto &str: symbols) {
  45. result.push_back(Php::Value(str));
  46. }
  47. return Php::Array(result);
  48. }
  49. std::string trim_braces(const std::string& input) {
  50. std::string result = input;
  51. if (result.size() >= 2 && result.substr(0, 2) == "{ ") {
  52. result = result.substr(2);
  53. }
  54. if (result.size() >= 2 && result.substr(result.size() - 2) == " }") {
  55. result = result.substr(0, result.size() - 2);
  56. }
  57. return result;
  58. }
  59. Php::Value HashTable::php_get_values() {
  60. Php::Array result;
  61. std::vector<std::pair<std::vector<char>, std::vector<char>>> entries;
  62. auto raw_table = bpf->get_table(tb_name);
  63. auto status = raw_table.get_table_offline_ptr(entries);
  64. if (status.code() != 0) {
  65. return result;
  66. }
  67. int i = 0;
  68. for (const auto& [key_data, val_data] : entries) {
  69. Php::Value phpKeyData(key_data.data(), key_data.size());
  70. Php::Value phpValData(val_data.data(), val_data.size());
  71. Php::Array entry;
  72. entry.set("key", phpKeyData);
  73. entry.set("value", phpValData);
  74. result.set(i++, entry);
  75. }
  76. return result;
  77. }
  78. void HashTable::php_clear() {
  79. auto raw_table = bpf->get_table(tb_name);
  80. raw_table.clear_table_non_atomic();
  81. }
  82. Php::Value PerCpuArrayTable::php_sum_value(Php::Parameters &param) {
  83. auto index = param[0].numericValue();
  84. std::vector<unsigned long> val;
  85. auto table = bpf->get_percpu_array_table<uint64_t>(tb_name);
  86. auto res = table.get_value(index, val);
  87. if (!res.ok()) {
  88. throw Php::Exception("Get value error in" + std::string(tb_name));
  89. }
  90. unsigned long long sum = 0;
  91. for (const auto &v: val) {
  92. sum += v;
  93. }
  94. Php::Value phpValue = static_cast<int64_t>(sum);
  95. return phpValue;
  96. }