table.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. auto pid = param[1].numericValue();
  37. auto table = bpf->get_stack_table(tb_name);
  38. auto symbols = table.get_stack_symbol((int) stack_id, (int) pid);
  39. std::vector<std::string> result;
  40. for (const auto &str: symbols) {
  41. result.push_back(Php::Value(str));
  42. }
  43. return Php::Array(result);
  44. }
  45. Php::Value HashTable::php_get_values() {
  46. Php::Array result;
  47. auto table = bpf->get_hash_table<uint64_t, uint64_t>(tb_name);
  48. auto entries = table.get_table_offline();
  49. for (const auto &[key, value]: entries) {
  50. result[key] = Php::Value(static_cast<int64_t>(value));
  51. }
  52. return result;
  53. }
  54. Php::Value PerCpuArrayTable::php_sum_value(Php::Parameters &param) {
  55. auto index = param[0].numericValue();
  56. std::vector<unsigned long> val;
  57. auto table = bpf->get_percpu_array_table<uint64_t>(tb_name);
  58. auto res = table.get_value(index, val);
  59. if (!res.ok()) {
  60. throw Php::Exception("Get value error in" + std::string(tb_name));
  61. }
  62. unsigned long long sum = 0;
  63. for (const auto &v: val) {
  64. sum += v;
  65. }
  66. Php::Value phpValue = static_cast<int64_t>(sum);
  67. return phpValue;
  68. }