| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include "table.h"
- //BaseTable::BaseTable(ebpf::BPF *bpf_instance, const std::string &tb_name)
- //: bpf(bpf_instance), tb_name(tb_name){}
- void BaseTable::printName() {
- std::cout << "Table name: " << tb_name << std::endl;
- }
- std::string cb_fn;
- void PerfEventArrayTable::callbackfn(void *cookie, void *data, int data_size) {
- Php::Value phpData((const char *) data, data_size);
- Php::call(cb_fn.c_str(), nullptr, phpData, data_size);
- }
- void PerfEventArrayTable::php_open_perf_buffer(Php::Parameters ¶ms) {
- cb_fn = params[0].stringValue();
- auto res = this->bpf->open_perf_buffer(this->tb_name, &PerfEventArrayTable::callbackfn);
- if (!res.ok()) {
- throw Php::Exception("open_perf_buffer error:" + res.msg());
- }
- }
- int PerfEventArrayTable::perf_buffer_poll(int timeout_ms) {
- return bpf->poll_perf_buffer(this->tb_name, timeout_ms); // perf_reader_event_read
- }
- Php::Value ArrayTable::php_get_value(Php::Parameters ¶m) {
- auto index = param[0].numericValue();
- // todo val type
- uint64_t val;
- auto table = bpf->get_array_table<uint64_t>(tb_name);
- auto res = table.get_value(index, val);
- if (!res.ok()) {
- throw Php::Exception("Get value error in" + std::string(tb_name));
- }
- Php::Value phpValue = static_cast<int64_t>(val);
- return phpValue;
- }
- Php::Value StackTraceTable::php_get_values(Php::Parameters ¶m) {
- auto stack_id = param[0].numericValue();
- auto pid = param[1].numericValue();
- auto table = bpf->get_stack_table(tb_name);
- auto symbols = table.get_stack_symbol((int) stack_id, (int) pid);
- std::vector<std::string> result;
- for (const auto &str: symbols) {
- result.push_back(Php::Value(str));
- }
- return Php::Array(result);
- }
- Php::Value HashTable::php_get_values() {
- Php::Array result;
- auto table = bpf->get_hash_table<uint64_t, uint64_t>(tb_name);
- auto entries = table.get_table_offline();
- for (const auto &[key, value]: entries) {
- result[key] = Php::Value(static_cast<int64_t>(value));
- }
- return result;
- }
- Php::Value PerCpuArrayTable::php_sum_value(Php::Parameters ¶m) {
- auto index = param[0].numericValue();
- std::vector<unsigned long> val;
- auto table = bpf->get_percpu_array_table<uint64_t>(tb_name);
- auto res = table.get_value(index, val);
- if (!res.ok()) {
- throw Php::Exception("Get value error in" + std::string(tb_name));
- }
- unsigned long long sum = 0;
- for (const auto &v: val) {
- sum += v;
- }
- Php::Value phpValue = static_cast<int64_t>(sum);
- return phpValue;
- }
|