diff --git a/package.json b/package.json index 9dd2755..78e5457 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "devtoolx", - "version": "0.1.8", + "version": "0.1.9", "description": "dev tools box", "main": "devtoolx.js", "bin": { diff --git a/src/memory/edge.cc b/src/memory/edge.cc index 6e39667..9b517f7 100644 --- a/src/memory/edge.cc +++ b/src/memory/edge.cc @@ -5,13 +5,13 @@ namespace snapshot_edge { Edge::Edge(snapshot_parser::SnapshotParser* parser): parser_(parser) {} -std::string Edge::GetType(long id, bool source) { +std::string Edge::GetType(int id, bool source) { int edge_field_length = parser_->edge_field_length; if(source && id % edge_field_length != 0) { Nan::ThrowTypeError(Nan::New("edge source id is wrong!").ToLocalChecked()); return "error"; } - long edge_source_index = source ? id : id * edge_field_length; + int edge_source_index = source ? id : id * edge_field_length; if(edge_source_index / edge_field_length >= parser_->edge_count) { Nan::ThrowTypeError(Nan::New("edge id larger than edges.length!").ToLocalChecked()); return "error"; @@ -21,13 +21,13 @@ std::string Edge::GetType(long id, bool source) { return types[type]; } -int Edge::GetTypeForInt(long id, bool source) { +int Edge::GetTypeForInt(int id, bool source) { int edge_field_length = parser_->edge_field_length; if(source && id % edge_field_length != 0) { Nan::ThrowTypeError(Nan::New("edge source id is wrong!").ToLocalChecked()); return -1; } - long edge_source_index = source ? id : id * edge_field_length; + int edge_source_index = source ? id : id * edge_field_length; if(edge_source_index / edge_field_length >= parser_->edge_count) { Nan::ThrowTypeError(Nan::New("edge id larger than edges.length!").ToLocalChecked()); return -1; @@ -35,19 +35,19 @@ int Edge::GetTypeForInt(long id, bool source) { return static_cast(parser_->edges[edge_source_index + parser_->edge_type_offset]); } -std::string Edge::GetNameOrIndex(long id, bool source) { +std::string Edge::GetNameOrIndex(int id, bool source) { int edge_field_length = parser_->edge_field_length; if(source && id % edge_field_length != 0) { Nan::ThrowTypeError(Nan::New("edge source id is wrong!").ToLocalChecked()); return "error"; } - long edge_source_index = source ? id : id * edge_field_length; + int edge_source_index = source ? id : id * edge_field_length; if(edge_source_index / edge_field_length >= parser_->edge_count) { Nan::ThrowTypeError(Nan::New("edge id larger than edges.length!").ToLocalChecked()); return "error"; } int type = static_cast(parser_->edges[edge_source_index + parser_->edge_type_offset]); - long name_or_index = static_cast(parser_->edges[edge_source_index + parser_->edge_name_or_index_offset]); + int name_or_index = static_cast(parser_->edges[edge_source_index + parser_->edge_name_or_index_offset]); if(type == KELEMENT) { return "[" + std::to_string(name_or_index) + "]"; } else if(type == KHIDDEN) { @@ -57,23 +57,23 @@ std::string Edge::GetNameOrIndex(long id, bool source) { }; } -long Edge::GetTargetNode(long id, bool source) { +int Edge::GetTargetNode(int id, bool source) { int edge_field_length = parser_->edge_field_length; if(source && id % edge_field_length != 0) { Nan::ThrowTypeError(Nan::New("edge source id is wrong!").ToLocalChecked()); return -1; } - long edge_source_index = source ? id : id * edge_field_length; + int edge_source_index = source ? id : id * edge_field_length; if(edge_source_index / edge_field_length >= parser_->edge_count) { Nan::ThrowTypeError(Nan::New("edge id larger than edges.length!").ToLocalChecked()); return -1; } - long target_node_source_id = static_cast(parser_->edges[edge_source_index + parser_->edge_to_node_offset]); + int target_node_source_id = static_cast(parser_->edges[edge_source_index + parser_->edge_to_node_offset]); int node_field_length = parser_->node_field_length; if(target_node_source_id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("target node source id is wrong!").ToLocalChecked()); return -1; } - return static_cast(target_node_source_id / node_field_length); + return static_cast(target_node_source_id / node_field_length); } } \ No newline at end of file diff --git a/src/memory/edge.h b/src/memory/edge.h index 994fc7a..c6ab1d0 100644 --- a/src/memory/edge.h +++ b/src/memory/edge.h @@ -24,10 +24,10 @@ class Edge { public: explicit Edge(snapshot_parser::SnapshotParser* parser); ~Edge(); - std::string GetType(long id, bool source); - int GetTypeForInt(long id, bool source); - std::string GetNameOrIndex(long id, bool source); - long GetTargetNode(long id, bool source); + std::string GetType(int id, bool source); + int GetTypeForInt(int id, bool source); + std::string GetNameOrIndex(int id, bool source); + int GetTargetNode(int id, bool source); private: snapshot_parser::SnapshotParser* parser_; }; diff --git a/src/memory/node.cc b/src/memory/node.cc index 09918cb..b43916c 100644 --- a/src/memory/node.cc +++ b/src/memory/node.cc @@ -5,22 +5,22 @@ namespace snapshot_node { Node::Node(snapshot_parser::SnapshotParser* parser): parser_(parser) {} -long Node::GetNodeId(long source) { +int Node::GetNodeId(int source) { int node_field_length = parser_->node_field_length; if(source % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return -1; } - return static_cast(source / node_field_length); + return static_cast(source / node_field_length); } -long Node::GetAddress(long id, bool source) { +long Node::GetAddress(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return -1; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return -1; @@ -28,13 +28,13 @@ long Node::GetAddress(long id, bool source) { return static_cast(parser_->nodes[node_source_index + parser_->node_address_offset]); } -std::string Node::GetType(long id, bool source) { +std::string Node::GetType(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return "error"; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return "error"; @@ -48,13 +48,13 @@ std::string Node::GetType(long id, bool source) { return types[type]; } -int Node::GetTypeForInt(long id, bool source) { +int Node::GetTypeForInt(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return -1; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return -1; @@ -62,7 +62,7 @@ int Node::GetTypeForInt(long id, bool source) { return static_cast(parser_->nodes[node_source_index + parser_->node_type_offset]); } -std::string Node::GetName(long id, bool source) { +std::string Node::GetName(int id, bool source) { if(id == parser_->root_index) { return "SYNTTETICROOT"; } @@ -71,62 +71,62 @@ std::string Node::GetName(long id, bool source) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return "error"; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return "error"; } json strings = parser_->strings; - long name = static_cast(parser_->nodes[node_source_index + parser_->node_name_offset]); + int name = static_cast(parser_->nodes[node_source_index + parser_->node_name_offset]); return strings[name]; } -long Node::GetNameForLong(long id, bool source) { +int Node::GetNameForInt(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return -1; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return -1; } - return static_cast(parser_->nodes[node_source_index + parser_->node_name_offset]); + return static_cast(parser_->nodes[node_source_index + parser_->node_name_offset]); } -long* Node::GetEdges(long id, bool source) { +int* Node::GetEdges(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); - return new long[0]; + return new int[0]; } - long node_ordinal_index = source ? id / node_field_length : id; + int node_ordinal_index = source ? id / node_field_length : id; if(node_ordinal_index >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); - return new long[0]; + return new int[0]; } - long first_edge_index = parser_->first_edge_indexes[node_ordinal_index]; - long next_first_edge_index = 0; + int first_edge_index = parser_->first_edge_indexes[node_ordinal_index]; + int next_first_edge_index = 0; if(node_ordinal_index + 1 >= parser_->node_count) { - next_first_edge_index = static_cast(parser_->edges.size()); + next_first_edge_index = static_cast(parser_->edges.size()); } else { next_first_edge_index = parser_->first_edge_indexes[node_ordinal_index + 1]; } - long* edges = new long[(next_first_edge_index - first_edge_index) / parser_->edge_field_length]; - for (long i = first_edge_index; i < next_first_edge_index; i += parser_->edge_field_length) { + int* edges = new int[(next_first_edge_index - first_edge_index) / parser_->edge_field_length]; + for (int i = first_edge_index; i < next_first_edge_index; i += parser_->edge_field_length) { edges[(i - first_edge_index) / parser_->edge_field_length] = i; } return edges; } -int Node::GetEdgeCount(long id, bool source) { +int Node::GetEdgeCount(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return -1; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return -1; @@ -135,17 +135,17 @@ int Node::GetEdgeCount(long id, bool source) { return static_cast(parser_->nodes[node_source_index + parser_->node_edge_count_offset]); } -long Node::GetSelfSize(long id, bool source) { +int Node::GetSelfSize(int id, bool source) { int node_field_length = parser_->node_field_length; if(source && id % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node source id is wrong!").ToLocalChecked()); return -1; } - long node_source_index = source ? id : id * node_field_length; + int node_source_index = source ? id : id * node_field_length; if(node_source_index / node_field_length >= parser_->node_count) { Nan::ThrowTypeError(Nan::New("node id larger than nodes.length!").ToLocalChecked()); return -1; } - return static_cast(parser_->nodes[node_source_index + parser_->node_self_size_offset]); + return static_cast(parser_->nodes[node_source_index + parser_->node_self_size_offset]); } } \ No newline at end of file diff --git a/src/memory/node.h b/src/memory/node.h index 924e972..2ded0e7 100644 --- a/src/memory/node.h +++ b/src/memory/node.h @@ -29,15 +29,15 @@ class Node { public: explicit Node(snapshot_parser::SnapshotParser* parser); ~Node(); - long GetNodeId(long source ); - long GetAddress(long id, bool source); - std::string GetType(long id, bool source); - int GetTypeForInt(long id, bool source); - std::string GetName(long id, bool source); - long GetNameForLong(long id, bool source); - long* GetEdges(long id, bool source); - int GetEdgeCount(long id, bool source); - long GetSelfSize(long id, bool source); + int GetNodeId(int source ); + long GetAddress(int id, bool source); + std::string GetType(int id, bool source); + int GetTypeForInt(int id, bool source); + std::string GetName(int id, bool source); + int GetNameForInt(int id, bool source); + int* GetEdges(int id, bool source); + int GetEdgeCount(int id, bool source); + int GetSelfSize(int id, bool source); private: snapshot_parser::SnapshotParser* parser_; }; diff --git a/src/memory/parser.cc b/src/memory/parser.cc index ab3083a..28ed69e 100644 --- a/src/memory/parser.cc +++ b/src/memory/parser.cc @@ -83,7 +83,7 @@ void Parser::Parse(const Nan::FunctionCallbackInfo& info) { } } -Local Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes get_node_type) { +Local Parser::GetNodeById_(int id, int current, int limit, GetNodeTypes get_node_type) { Local node = Nan::New(); node->Set(Nan::New("id").ToLocalChecked(), Nan::New(id)); std::string type = snapshot_parser->node_util->GetType(id, false); @@ -92,7 +92,7 @@ Local Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes node->Set(Nan::New("name").ToLocalChecked(), Nan::New(name).ToLocalChecked()); std::string address = "@" + std::to_string(snapshot_parser->node_util->GetAddress(id, false)); node->Set(Nan::New("address").ToLocalChecked(), Nan::New(address).ToLocalChecked()); - long self_size = snapshot_parser->node_util->GetSelfSize(id, false); + int self_size = snapshot_parser->node_util->GetSelfSize(id, false); node->Set(Nan::New("self_size").ToLocalChecked(), Nan::New(self_size)); int distance = snapshot_parser->GetDistance(id); node->Set(Nan::New("distance").ToLocalChecked(), Nan::New(distance)); @@ -100,7 +100,7 @@ Local Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes node->Set(Nan::New("is_gcroot").ToLocalChecked(), Nan::New(is_gcroot)); // get edges if(get_node_type == KALL || get_node_type == KEDGES) { - long* edges_local = snapshot_parser->node_util->GetEdges(id, false); + int* edges_local = snapshot_parser->node_util->GetEdges(id, false); int edges_length = snapshot_parser->node_util->GetEdgeCount(id, false); int start_edge_index = current; int stop_edge_index = current + limit; @@ -115,7 +115,7 @@ Local Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes Local edge = Nan::New(); std::string edge_type = snapshot_parser->edge_util->GetType(edges_local[i], true); std::string name_or_index = snapshot_parser->edge_util->GetNameOrIndex(edges_local[i], true); - long to_node = snapshot_parser->edge_util->GetTargetNode(edges_local[i], true); + int to_node = snapshot_parser->edge_util->GetTargetNode(edges_local[i], true); edge->Set(Nan::New("type").ToLocalChecked(), Nan::New(edge_type).ToLocalChecked()); edge->Set(Nan::New("name_or_index").ToLocalChecked(), Nan::New(name_or_index).ToLocalChecked()); edge->Set(Nan::New("to_node").ToLocalChecked(), Nan::New(to_node)); @@ -132,7 +132,7 @@ Local Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes } // get retainers if(get_node_type == KALL || get_node_type == KRETAINERS) { - long* retainers_local = snapshot_parser->GetRetainers(id); + int* retainers_local = snapshot_parser->GetRetainers(id); int retainers_length = snapshot_parser->GetRetainersCount(id); int start_retainer_index = current; int stop_retainer_index = current + limit; @@ -144,8 +144,8 @@ Local Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes } Local retainers = Nan::New(stop_retainer_index - start_retainer_index); for(int i = start_retainer_index; i < stop_retainer_index; i++) { - long node = retainers_local[i * 2]; - long edge = retainers_local[i * 2 + 1]; + int node = retainers_local[i * 2]; + int edge = retainers_local[i * 2 + 1]; Local retainer = Nan::New(); std::string edge_type = snapshot_parser->edge_util->GetType(edge, true); std::string name_or_index = snapshot_parser->edge_util->GetNameOrIndex(edge, true); @@ -171,9 +171,9 @@ void Parser::GetNodeId(const Nan::FunctionCallbackInfo& info) { Nan::ThrowTypeError(Nan::New("argument must be number!").ToLocalChecked()); return; } - long source = static_cast(info[0]->ToInteger()->Value()); + int source = static_cast(info[0]->ToInteger()->Value()); Parser* parser = ObjectWrap::Unwrap(info.Holder()); - long nodeid = parser->snapshot_parser->node_util->GetNodeId(source); + int nodeid = parser->snapshot_parser->node_util->GetNodeId(source); info.GetReturnValue().Set(Nan::New(nodeid)); } @@ -210,7 +210,7 @@ void Parser::GetNodeByOrdinalId(const Nan::FunctionCallbackInfo& info) { } int error_id_count = 0; for(int i = 0; i < length; i++) { - long id = static_cast(list->Get(Nan::New(i))->ToInteger()->Value()); + int id = static_cast(list->Get(Nan::New(i))->ToInteger()->Value()); if(id >= parser->snapshot_parser->node_count) { error_id_count++; break; @@ -239,7 +239,7 @@ void Parser::GetNodeByAddress(const Nan::FunctionCallbackInfo& info) { Nan::ThrowTypeError(Nan::New("argument 0 must be startwith \"@\"!").ToLocalChecked()); return; } - long id = parser->snapshot_parser->SearchOrdinalByAddress(atol((*addr) + 1)); + int id = parser->snapshot_parser->SearchOrdinalByAddress(atoi((*addr) + 1)); if(id == -1) { std::string addrs = *addr; std::string error = "address \"" + addrs + "\" is wrong!"; @@ -270,7 +270,7 @@ void Parser::GetNodeIdByAddress(const Nan::FunctionCallbackInfo& info) { return; } Parser* parser = ObjectWrap::Unwrap(info.Holder()); - long id = parser->snapshot_parser->SearchOrdinalByAddress(atol((*addr) + 1)); + int id = parser->snapshot_parser->SearchOrdinalByAddress(atoi((*addr) + 1)); if(id == -1) { std::string addrs = *addr; std::string error = "address \"" + addrs + "\" is wrong!"; diff --git a/src/memory/parser.h b/src/memory/parser.h index 9ac5e99..81c1465 100644 --- a/src/memory/parser.h +++ b/src/memory/parser.h @@ -26,7 +26,7 @@ class Parser : public node::ObjectWrap { private: explicit Parser(char* filename, int filelength); ~Parser(); - v8::Local GetNodeById_(long id, int current, int limit, GetNodeTypes type); + v8::Local GetNodeById_(int id, int current, int limit, GetNodeTypes type); static void New(const Nan::FunctionCallbackInfo& info); static void GetFileName(const Nan::FunctionCallbackInfo& info); static void Parse(const Nan::FunctionCallbackInfo& info); diff --git a/src/memory/snapshot_parser.cc b/src/memory/snapshot_parser.cc index c2f75df..923bc23 100644 --- a/src/memory/snapshot_parser.cc +++ b/src/memory/snapshot_parser.cc @@ -13,8 +13,8 @@ SnapshotParser::SnapshotParser(json profile) { json edge_fields = snapshot["meta"]["edge_fields"]; node_field_length = node_fields.size(); edge_field_length = edge_fields.size(); - node_count = nodes.size() / node_field_length; - edge_count = edges.size() / edge_field_length; + node_count = static_cast(nodes.size() / node_field_length); + edge_count = static_cast(edges.size() / edge_field_length); node_types = snapshot["meta"]["node_types"][0]; edge_types = snapshot["meta"]["edge_types"][0]; node_type_offset = IndexOf(node_fields, "type"); @@ -26,7 +26,7 @@ SnapshotParser::SnapshotParser(json profile) { edge_type_offset = IndexOf(edge_fields, "type"); edge_name_or_index_offset = IndexOf(edge_fields, "name_or_index"); edge_to_node_offset = IndexOf(edge_fields, "to_node"); - edge_from_node = new long[edge_count]; + edge_from_node = new int[edge_count]; first_edge_indexes = GetFirstEdgeIndexes(); node_util = new snapshot_node::Node(this); edge_util = new snapshot_edge::Edge(this); @@ -44,9 +44,9 @@ int SnapshotParser::IndexOf(json array, std::string target) { return -1; } -long* SnapshotParser::GetFirstEdgeIndexes() { - long* first_edge_indexes = new long[node_count]; - for(long node_ordinal = 0, edge_index = 0; node_ordinal < node_count; node_ordinal++) { +int* SnapshotParser::GetFirstEdgeIndexes() { + int* first_edge_indexes = new int[node_count]; + for(int node_ordinal = 0, edge_index = 0; node_ordinal < node_count; node_ordinal++) { first_edge_indexes[node_ordinal] = edge_index; int offset = static_cast(nodes[node_ordinal * node_field_length + node_edge_count_offset]) * edge_field_length; for(int i = edge_index; i < offset; i += edge_field_length) { @@ -58,7 +58,7 @@ long* SnapshotParser::GetFirstEdgeIndexes() { } void SnapshotParser::CreateAddressMap() { - for(long ordinal = 0; ordinal < node_count; ordinal++) { + for(int ordinal = 0; ordinal < node_count; ordinal++) { long address = node_util->GetAddress(ordinal, false); address_map_.insert(AddressMap::value_type(address, ordinal)); } @@ -68,32 +68,32 @@ void SnapshotParser::ClearAddressMap() { address_map_.clear(); } -long SnapshotParser::SearchOrdinalByAddress(long address) { +int SnapshotParser::SearchOrdinalByAddress(long address) { int count = address_map_.count(address); if(count == 0) { return -1; } - long ordinal = address_map_.at(address); + int ordinal = address_map_.at(address); return ordinal; } void SnapshotParser::BuildTotalRetainer() { - retaining_nodes_ = new long[edge_count] {0}; - retaining_edges_ = new long[edge_count] {0}; - first_retainer_index_ = new long[node_count + 1] {0}; + retaining_nodes_ = new int[edge_count] {0}; + retaining_edges_ = new int[edge_count] {0}; + first_retainer_index_ = new int[node_count + 1] {0}; // every node's retainer count - for(long to_node_field_index = edge_to_node_offset, l = static_cast(edges.size()); to_node_field_index < l; to_node_field_index += edge_field_length) { - long to_node_index = static_cast(edges[to_node_field_index]); + for(int to_node_field_index = edge_to_node_offset, l = static_cast(edges.size()); to_node_field_index < l; to_node_field_index += edge_field_length) { + int to_node_index = static_cast(edges[to_node_field_index]); if(to_node_index % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("node index id is wrong!").ToLocalChecked()); return; } - long ordinal_id = to_node_index / node_field_length; + int ordinal_id = to_node_index / node_field_length; first_retainer_index_[ordinal_id] += 1; } // set first retainer index - for(long i = 0, first_unused_retainer_slot = 0; i < node_count; i++) { - long retainers_count = first_retainer_index_[i]; + for(int i = 0, first_unused_retainer_slot = 0; i < node_count; i++) { + int retainers_count = first_retainer_index_[i]; first_retainer_index_[i] = first_unused_retainer_slot; retaining_nodes_[first_unused_retainer_slot] = retainers_count; first_unused_retainer_slot += retainers_count; @@ -101,18 +101,18 @@ void SnapshotParser::BuildTotalRetainer() { // for (index ~ index + 1) first_retainer_index_[node_count] = edge_count; // set retaining slot - long next_node_first_edge_index = first_edge_indexes[0]; - for(long src_node_ordinal = 0; src_node_ordinal < node_count; src_node_ordinal++) { - long first_edge_index = next_node_first_edge_index; + int next_node_first_edge_index = first_edge_indexes[0]; + for(int src_node_ordinal = 0; src_node_ordinal < node_count; src_node_ordinal++) { + int first_edge_index = next_node_first_edge_index; next_node_first_edge_index = first_edge_indexes[src_node_ordinal + 1]; - for(long edge_index = first_edge_index; edge_index < next_node_first_edge_index; edge_index += edge_field_length) { - long to_node_index = static_cast(edges[edge_index + edge_to_node_offset]); + for(int edge_index = first_edge_index; edge_index < next_node_first_edge_index; edge_index += edge_field_length) { + int to_node_index = static_cast(edges[edge_index + edge_to_node_offset]); if(to_node_index % node_field_length != 0) { Nan::ThrowTypeError(Nan::New("to_node id is wrong!").ToLocalChecked()); return; } - long first_retainer_slot_index = first_retainer_index_[to_node_index / node_field_length]; - long next_unused_retainer_slot_index = first_retainer_slot_index + (--retaining_nodes_[first_retainer_slot_index]); + int first_retainer_slot_index = first_retainer_index_[to_node_index / node_field_length]; + int next_unused_retainer_slot_index = first_retainer_slot_index + (--retaining_nodes_[first_retainer_slot_index]); // save retainer & edge retaining_nodes_[next_unused_retainer_slot_index] = src_node_ordinal; retaining_edges_[next_unused_retainer_slot_index] = edge_index; @@ -120,21 +120,21 @@ void SnapshotParser::BuildTotalRetainer() { } } -int SnapshotParser::GetRetainersCount(long id) { - long first_retainer_index = first_retainer_index_[id]; - long next_retainer_index = first_retainer_index_[id + 1]; +int SnapshotParser::GetRetainersCount(int id) { + int first_retainer_index = first_retainer_index_[id]; + int next_retainer_index = first_retainer_index_[id + 1]; // count may not be larger than 2^31 return static_cast(next_retainer_index - first_retainer_index); } -long* SnapshotParser::GetRetainers(long id) { - long first_retainer_index = first_retainer_index_[id]; - long next_retainer_index = first_retainer_index_[id + 1]; +int* SnapshotParser::GetRetainers(int id) { + int first_retainer_index = first_retainer_index_[id]; + int next_retainer_index = first_retainer_index_[id + 1]; int length = static_cast(next_retainer_index - first_retainer_index); - long* retainers = new long[length * 2]; - for(long i = first_retainer_index; i < next_retainer_index; i++) { - long node = retaining_nodes_[i]; - long edge = retaining_edges_[i]; + int* retainers = new int[length * 2]; + for(int i = first_retainer_index; i < next_retainer_index; i++) { + int node = retaining_nodes_[i]; + int edge = retaining_edges_[i]; retainers[(i - first_retainer_index) * 2] = node; retainers[(i - first_retainer_index) * 2 + 1] = edge; } @@ -149,7 +149,7 @@ void SnapshotParser::EnqueueNode_(snapshot_distance_t* t) { *(t->node_to_visit_length) += 1; } -bool SnapshotParser::Filter(long ordinal, long edge) { +bool SnapshotParser::Filter(int ordinal, int edge) { int node_type = node_util->GetTypeForInt(ordinal, false); if(node_type == snapshot_node::NodeTypes::KHIDDEN) { std::string edge_name = edge_util->GetNameOrIndex(edge, true); @@ -171,12 +171,12 @@ bool SnapshotParser::Filter(long ordinal, long edge) { } void SnapshotParser::ForEachRoot_(void (*action)(snapshot_distance_t* t), snapshot_distance_t* user_root, bool user_root_only) { - std::unordered_map visit_nodes; - long gc_roots = -1; - long* edges = node_util->GetEdges(root_index, false); + std::unordered_map visit_nodes; + int gc_roots = -1; + int* edges = node_util->GetEdges(root_index, false); int length = node_util->GetEdgeCount(root_index, false); for(int i = 0; i < length; i++) { - long target_node = edge_util->GetTargetNode(*(edges + i), true); + int target_node = edge_util->GetTargetNode(*(edges + i), true); std::string node_name = node_util->GetName(target_node, false); std::string gc_root_name = "(GC roots)"; if(strcmp(node_name.c_str(), gc_root_name.c_str()) == 0) { @@ -188,7 +188,7 @@ void SnapshotParser::ForEachRoot_(void (*action)(snapshot_distance_t* t), snapsh if(user_root_only) { // iterator the "true" root, set user root distance 1 -> global for(int i = 0; i < length; i++) { - long target_node = edge_util->GetTargetNode(*(edges + i), true); + int target_node = edge_util->GetTargetNode(*(edges + i), true); std::string node_name = node_util->GetName(target_node, false); int type = node_util->GetTypeForInt(target_node, false); // type != synthetic, means user root @@ -196,33 +196,33 @@ void SnapshotParser::ForEachRoot_(void (*action)(snapshot_distance_t* t), snapsh if(visit_nodes.count(target_node) == 0) { user_root->ordinal = target_node; action(user_root); - visit_nodes.insert(std::unordered_map::value_type(target_node, true)); + visit_nodes.insert(std::unordered_map::value_type(target_node, true)); } } } // set user root gc roots -> synthetic roots -> true roots - // long* sub_root_edges = node_util->GetEdges(gc_roots, false); + // int* sub_root_edges = node_util->GetEdges(gc_roots, false); // int sub_root_edge_length = node_util->GetEdgeCount(gc_roots, false); // for(int i = 0; i < sub_root_edge_length; i++) { - // long sub_root_ordinal = edge_util->GetTargetNode(*(sub_root_edges + i), true); - // long* sub2_root_edges = node_util->GetEdges(sub_root_ordinal, false); + // int sub_root_ordinal = edge_util->GetTargetNode(*(sub_root_edges + i), true); + // int* sub2_root_edges = node_util->GetEdges(sub_root_ordinal, false); // int sub2_root_edge_length = node_util->GetEdgeCount(sub_root_ordinal, false); // for(int j = 0; j < sub2_root_edge_length; j++) { - // long sub2_root_ordinal = edge_util->GetTargetNode(*(sub2_root_edges + j), true); + // int sub2_root_ordinal = edge_util->GetTargetNode(*(sub2_root_edges + j), true); // // mark sub sub gc roots // if(visit_nodes.count(sub2_root_ordinal) == 0) { // user_root->ordinal = sub2_root_ordinal; // action(user_root); - // visit_nodes.insert(std::unordered_map::value_type(sub2_root_ordinal, true)); + // visit_nodes.insert(std::unordered_map::value_type(sub2_root_ordinal, true)); // } // } // } } else { - long* sub_root_edges = node_util->GetEdges(gc_roots, false); + int* sub_root_edges = node_util->GetEdges(gc_roots, false); int sub_root_edge_length = node_util->GetEdgeCount(gc_roots, false); for(int i = 0; i < sub_root_edge_length; i++) { - long sub_root_ordinal = edge_util->GetTargetNode(*(sub_root_edges + i), true); - long* sub2_root_edges = node_util->GetEdges(sub_root_ordinal, false); + int sub_root_ordinal = edge_util->GetTargetNode(*(sub_root_edges + i), true); + int* sub2_root_edges = node_util->GetEdges(sub_root_ordinal, false); int sub2_root_edge_length = node_util->GetEdgeCount(sub_root_ordinal, false); bool need_add_gc_root = true; std::string sub_root_name = node_util->GetName(sub_root_ordinal, false); @@ -232,12 +232,12 @@ void SnapshotParser::ForEachRoot_(void (*action)(snapshot_distance_t* t), snapsh need_add_gc_root = false; } for(int j = 0; j < sub2_root_edge_length; j++) { - long sub2_root_ordinal = edge_util->GetTargetNode(*(sub2_root_edges + j), true); + int sub2_root_ordinal = edge_util->GetTargetNode(*(sub2_root_edges + j), true); // mark sub sub gc roots if(visit_nodes.count(sub2_root_ordinal) == 0) { user_root->ordinal = sub2_root_ordinal; action(user_root); - visit_nodes.insert(std::unordered_map::value_type(sub2_root_ordinal, true)); + visit_nodes.insert(std::unordered_map::value_type(sub2_root_ordinal, true)); } // add gc root if(need_add_gc_root) { @@ -252,28 +252,28 @@ void SnapshotParser::ForEachRoot_(void (*action)(snapshot_distance_t* t), snapsh if(visit_nodes.count(sub_root_ordinal) == 0) { user_root->ordinal = sub_root_ordinal; action(user_root); - visit_nodes.insert(std::unordered_map::value_type(sub_root_ordinal, true)); + visit_nodes.insert(std::unordered_map::value_type(sub_root_ordinal, true)); } } // mark sub roots for(int i = 0; i < length; i++) { - long target_node = edge_util->GetTargetNode(*(edges + i), true); + int target_node = edge_util->GetTargetNode(*(edges + i), true); if(visit_nodes.count(target_node) == 0) { user_root->ordinal = target_node; action(user_root); - visit_nodes.insert(std::unordered_map::value_type(target_node, true)); + visit_nodes.insert(std::unordered_map::value_type(target_node, true)); } } } } -void SnapshotParser::BFS_(long* node_to_visit, int node_to_visit_length) { +void SnapshotParser::BFS_(int* node_to_visit, int node_to_visit_length) { int index = 0; int temp = 0; while(index < node_to_visit_length) { - long ordinal = node_to_visit[index++]; + int ordinal = node_to_visit[index++]; int distance = node_distances_[ordinal] + 1; - long* edges = node_util->GetEdges(ordinal, false); + int* edges = node_util->GetEdges(ordinal, false); int edge_length = node_util->GetEdgeCount(ordinal, false); for(int i = 0; i < edge_length; i++) { int edge_type = edge_util->GetTypeForInt(*(edges + i), true); @@ -300,10 +300,10 @@ void SnapshotParser::BFS_(long* node_to_visit, int node_to_visit_length) { void SnapshotParser::BuildDistances() { node_distances_ = new int[node_count]; - for(long i = 0; i < node_count; i++) { + for(int i = 0; i < node_count; i++) { *(node_distances_ + i) = NO_DISTANCE; } - long* node_to_visit = new long[node_count] {0}; + int* node_to_visit = new int[node_count] {0}; int node_to_visit_length = 0; // add user root snapshot_distance_t* user_root = new snapshot_distance_t; @@ -321,11 +321,11 @@ void SnapshotParser::BuildDistances() { user_root = nullptr; } -int SnapshotParser::GetDistance(long id) { +int SnapshotParser::GetDistance(int id) { return node_distances_[id]; } -int SnapshotParser::IsGCRoot(long id) { +int SnapshotParser::IsGCRoot(int id) { int count = gcroots_map_.count(id); if(count == 0) return 0; diff --git a/src/memory/snapshot_parser.h b/src/memory/snapshot_parser.h index 6438db2..27382d7 100644 --- a/src/memory/snapshot_parser.h +++ b/src/memory/snapshot_parser.h @@ -13,14 +13,14 @@ using nlohmann::json; typedef struct { int distance; - long ordinal; - long* node_to_visit; + int ordinal; + int* node_to_visit; int* node_to_visit_length; int* node_distances_; } snapshot_distance_t; -typedef std::unordered_map AddressMap; -typedef std::unordered_map GCRootsMap; +typedef std::unordered_map AddressMap; +typedef std::unordered_map GCRootsMap; const int NO_DISTANCE = -5; const int BASE_SYSTEMDISTANCE = 100000000; @@ -31,13 +31,13 @@ class SnapshotParser { ~SnapshotParser(); void CreateAddressMap(); void ClearAddressMap(); - long SearchOrdinalByAddress(long address); + int SearchOrdinalByAddress(long address); void BuildTotalRetainer(); - int GetRetainersCount(long id); - long* GetRetainers(long id); + int GetRetainersCount(int id); + int* GetRetainers(int id); void BuildDistances(); - int GetDistance(long id); - int IsGCRoot(long id); + int GetDistance(int id); + int IsGCRoot(int id); static int IndexOf(json array, std::string target); json nodes; json edges; @@ -46,8 +46,8 @@ class SnapshotParser { int root_index = 0; int node_field_length; int edge_field_length; - long node_count; - long edge_count; + int node_count; + int edge_count; json node_types; json edge_types; int node_type_offset; @@ -59,26 +59,26 @@ class SnapshotParser { int edge_type_offset; int edge_name_or_index_offset; int edge_to_node_offset; - long* edge_from_node; - long* first_edge_indexes; + int* edge_from_node; + int* first_edge_indexes; snapshot_node::Node* node_util; snapshot_edge::Edge* edge_util; int gcroots; private: - long* GetFirstEdgeIndexes(); + int* GetFirstEdgeIndexes(); static void EnqueueNode_(snapshot_distance_t* t); void ForEachRoot_(void (*action)(snapshot_distance_t* t), snapshot_distance_t* user_root, bool user_root_only); - void BFS_(long* node_to_visit, int node_to_visit_length); - bool Filter(long ordinal, long edge); + void BFS_(int* node_to_visit, int node_to_visit_length); + bool Filter(int ordinal, int edge); // address -> node ordinal id AddressMap address_map_; // ordinal id -> bool GCRootsMap gcroots_map_; // total retainers - long* retaining_nodes_; - long* retaining_edges_; - long* first_retainer_index_; + int* retaining_nodes_; + int* retaining_edges_; + int* first_retainer_index_; int* node_distances_; }; }