heap_snapshot.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/common/heap_snapshot.h"
  5. #include "v8/include/v8-profiler.h"
  6. namespace {
  7. class HeapSnapshotOutputStream : public v8::OutputStream {
  8. public:
  9. explicit HeapSnapshotOutputStream(base::File* file) : file_(file) {
  10. DCHECK(file_);
  11. }
  12. bool IsComplete() const { return is_complete_; }
  13. // v8::OutputStream
  14. int GetChunkSize() override { return 65536; }
  15. void EndOfStream() override { is_complete_ = true; }
  16. v8::OutputStream::WriteResult WriteAsciiChunk(char* data, int size) override {
  17. auto bytes_written = file_->WriteAtCurrentPos(data, size);
  18. return bytes_written == size ? kContinue : kAbort;
  19. }
  20. private:
  21. base::File* file_ = nullptr;
  22. bool is_complete_ = false;
  23. };
  24. } // namespace
  25. namespace atom {
  26. bool TakeHeapSnapshot(v8::Isolate* isolate, base::File* file) {
  27. DCHECK(isolate);
  28. DCHECK(file);
  29. if (!file->IsValid())
  30. return false;
  31. auto* snapshot = isolate->GetHeapProfiler()->TakeHeapSnapshot();
  32. if (!snapshot)
  33. return false;
  34. HeapSnapshotOutputStream stream(file);
  35. snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON);
  36. const_cast<v8::HeapSnapshot*>(snapshot)->Delete();
  37. return stream.IsComplete();
  38. }
  39. } // namespace atom