Files
sdk/runtime/vm/source_report.h
T
Jens Johansen 6c19a8fc81 [VM] Read and report constant constructor coverage from dill (2nd try)
This CL makes use of the now included constant constructor coverage
in the dill file.

It works like this:
* When the CFE evaluates constants, every constant constructor
  invocation evaluated saves the reference to the constructor in the
  `Source` (from the Components uri to source table) for the callers
  Library.
* This data is loaded into the VM in a "raw" format.
* When a request for coverage comes in, the VM - on top of the normal
  coverage processing - goes through all scripts to find constant
  constructor coverage for the requested script and offset. Note that
  all scripts must be checked because library A can have evaluated a
  constructor from library B - so even if only coverage for library B
  was requested, library A has to be checked.
  For all constructors found the start and end position is reported as
  covered. Note that this does not mark any initializes and there are
  (at least currently) no good way of marking which initializes were
  evaluated (because it has to be stable across edits even when the
  `advanced invalidation feature` is enabled).
* Note that the reason for the coverage to work on references - as
  hinted above - is because we want it to be stable across hot reloads
  even if/when advanced invalidation is enabled. This means, that
  library A cannot record "positional coverage" for library B because
  library B might get (for instance) new comments that will make any old
  offsets invalid. By using references we always lookup in the current
  world and use the correct offsets.

https://github.com/dart-lang/sdk/issues/38934

TEST=Existing test suite, new tests for the new coverage added.

Change-Id: I29531247a4b91a99d9a459cfdefbb9798e9c948f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175246
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2020-12-08 10:47:43 +00:00

143 lines
4.6 KiB
C++

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_SOURCE_REPORT_H_
#define RUNTIME_VM_SOURCE_REPORT_H_
#include "vm/globals.h"
#if !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
#include "vm/allocation.h"
#include "vm/flags.h"
#include "vm/hash_map.h"
#include "vm/object.h"
#include "vm/profiler_service.h"
#include "vm/token_position.h"
namespace dart {
// A SourceReport object is used to generate reports about the program
// source code, with information associated with source token
// positions. There are multiple possible kinds of reports.
class SourceReport {
public:
enum ReportKind {
kCallSites = 0x1,
kCoverage = 0x2,
kPossibleBreakpoints = 0x4,
kProfile = 0x8,
};
static const char* kCallSitesStr;
static const char* kCoverageStr;
static const char* kPossibleBreakpointsStr;
static const char* kProfileStr;
enum CompileMode { kNoCompile, kForceCompile };
// report_set is a bitvector indicating which reports to generate
// (e.g. kCallSites | kCoverage).
explicit SourceReport(intptr_t report_set, CompileMode compile = kNoCompile);
~SourceReport();
// Generate a source report for (some subrange of) a script.
//
// If script is null, then the report is generated for all scripts
// in the isolate.
void PrintJSON(JSONStream* js,
const Script& script,
TokenPosition start_pos = TokenPosition::kNoSource,
TokenPosition end_pos = TokenPosition::kNoSource);
private:
void ClearScriptTable();
void Init(Thread* thread,
const Script* script,
TokenPosition start_pos,
TokenPosition end_pos);
Thread* thread() const { return thread_; }
Zone* zone() const { return thread_->zone(); }
Isolate* isolate() const { return thread_->isolate(); }
bool IsReportRequested(ReportKind report_kind);
bool ShouldSkipFunction(const Function& func);
bool ShouldSkipField(const Field& field);
intptr_t GetScriptIndex(const Script& script);
bool ScriptIsLoadedByLibrary(const Script& script, const Library& lib);
void PrintCallSitesData(JSONObject* jsobj,
const Function& func,
const Code& code);
void PrintCoverageData(JSONObject* jsobj,
const Function& func,
const Code& code);
void PrintPossibleBreakpointsData(JSONObject* jsobj,
const Function& func,
const Code& code);
void PrintProfileData(JSONObject* jsobj, ProfileFunction* profile_function);
#if defined(DEBUG)
void VerifyScriptTable();
#endif
void PrintScriptTable(JSONArray* jsarr);
void VisitFunction(JSONArray* jsarr, const Function& func);
void VisitField(JSONArray* jsarr, const Field& field);
void VisitLibrary(JSONArray* jsarr, const Library& lib);
void VisitClosures(JSONArray* jsarr);
// An entry in the script table.
struct ScriptTableEntry {
ScriptTableEntry() : key(NULL), index(-1), script(NULL) {}
const String* key;
intptr_t index;
const Script* script;
};
// Needed for DirectChainedHashMap.
struct ScriptTableTrait {
typedef ScriptTableEntry* Value;
typedef const ScriptTableEntry* Key;
typedef ScriptTableEntry* Pair;
static Key KeyOf(Pair kv) { return kv; }
static Value ValueOf(Pair kv) { return kv; }
static inline intptr_t Hashcode(Key key) { return key->key->Hash(); }
static inline bool IsKeyEqual(Pair kv, Key key) {
return kv->script->raw() == key->script->raw();
}
};
void CollectAllScripts(
DirectChainedHashMap<ScriptTableTrait>* local_script_table,
GrowableArray<ScriptTableEntry*>* local_script_table_entries);
void CleanupCollectedScripts(
DirectChainedHashMap<ScriptTableTrait>* local_script_table,
GrowableArray<ScriptTableEntry*>* local_script_table_entries);
void CollectConstConstructorCoverageFromScripts(
GrowableArray<ScriptTableEntry*>* local_script_table_entries,
JSONArray* ranges);
intptr_t report_set_;
CompileMode compile_mode_;
Thread* thread_;
const Script* script_;
TokenPosition start_pos_;
TokenPosition end_pos_;
Profile profile_;
GrowableArray<ScriptTableEntry*> script_table_entries_;
DirectChainedHashMap<ScriptTableTrait> script_table_;
intptr_t next_script_index_;
};
} // namespace dart
#endif // !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME)
#endif // RUNTIME_VM_SOURCE_REPORT_H_