Global Metrics

path: .metrics.cyclomatic.sum
old: 324.0
new: 36.0

path: .metrics.cyclomatic.average
old: 24.923076923076923
new: 1.0285714285714285

path: .metrics.nargs.sum
old: 87.0
new: 9.0

path: .metrics.nargs.average
old: 7.25
new: 0.2903225806451613

path: .metrics.cognitive.average
old: 55.25
new: 0.03225806451612903

path: .metrics.cognitive.sum
old: 663.0
new: 1.0

path: .metrics.nom.functions
old: 12.0
new: 31.0

path: .metrics.nom.total
old: 12.0
new: 31.0

path: .metrics.loc.lloc
old: 669.0
new: 30.0

path: .metrics.loc.cloc
old: 79.0
new: 53.0

path: .metrics.loc.ploc
old: 1168.0
new: 131.0

path: .metrics.loc.blank
old: 202.0
new: 0.0

path: .metrics.loc.sloc
old: 1449.0
new: 183.0

path: .metrics.mi.mi_sei
old: -141.85000544031033
new: 15.435403121724754

path: .metrics.mi.mi_original
old: -80.98277340178021
new: 34.968611798651864

path: .metrics.mi.mi_visual_studio
old: 0.0
new: 20.44948058400694

path: .metrics.halstead.effort
old: 26117280.783673976
new: 70220.84799383747

path: .metrics.halstead.N1
old: 6032.0
new: 364.0

path: .metrics.halstead.purity_ratio
old: 0.3212918999186587
new: 1.5372202746581358

path: .metrics.halstead.difficulty
old: 277.6551724137931
new: 16.8

path: .metrics.halstead.n2
old: 377.0
new: 120.0

path: .metrics.halstead.vocabulary
old: 421.0
new: 138.0

path: .metrics.halstead.level
old: 0.003601589667163438
new: 0.05952380952380952

path: .metrics.halstead.N2
old: 4758.0
new: 224.0

path: .metrics.halstead.estimated_program_length
old: 3466.7396001223274
new: 903.8855214989838

path: .metrics.halstead.time
old: 1450960.043537443
new: 3901.1582218798594

path: .metrics.halstead.volume
old: 94063.7286048864
new: 4179.812380585563

path: .metrics.halstead.bugs
old: 29.342518294660128
new: 0.5673566180007015

path: .metrics.halstead.length
old: 10790.0
new: 588.0

path: .metrics.halstead.n1
old: 44.0
new: 18.0

path: .metrics.nexits.sum
old: 11.0
new: 30.0

path: .metrics.nexits.average
old: 0.9166666666666666
new: 0.967741935483871

Spaces Data

Minimal test - lines (23, 181)

path: .spaces[0].metrics.mi.mi_original
old: 71.76672212835221
new: 37.551581264045424

path: .spaces[0].metrics.mi.mi_sei
old: 28.85484071746187
new: 16.746397621420726

path: .spaces[0].metrics.mi.mi_visual_studio
old: 41.96884334991358
new: 21.95998904330142

path: .spaces[0].metrics.cyclomatic.average
old: 10.0
new: 1.0294117647058822

path: .spaces[0].metrics.cyclomatic.sum
old: 10.0
new: 35.0

path: .spaces[0].metrics.cognitive.sum
old: 11.0
new: 1.0

path: .spaces[0].metrics.cognitive.average
old: 11.0
new: 0.03225806451612903

path: .spaces[0].metrics.halstead.n1
old: 16.0
new: 18.0

path: .spaces[0].metrics.halstead.bugs
old: 0.23664083920676576
new: 0.5661475276637874

path: .spaces[0].metrics.halstead.n2
old: 22.0
new: 116.0

path: .spaces[0].metrics.halstead.difficulty
old: 21.454545454545453
new: 16.99137931034483

path: .spaces[0].metrics.halstead.N2
old: 59.0
new: 219.0

path: .spaces[0].metrics.halstead.estimated_program_length
old: 162.10749561002052
new: 870.5844454607599

path: .spaces[0].metrics.halstead.level
old: 0.046610169491525424
new: 0.05885337392186707

path: .spaces[0].metrics.halstead.effort
old: 18915.439095728296
new: 69996.49677698873

path: .spaces[0].metrics.halstead.N1
old: 109.0
new: 364.0

path: .spaces[0].metrics.halstead.time
old: 1050.857727540461
new: 3888.694265388263

path: .spaces[0].metrics.halstead.length
old: 168.0
new: 583.0

path: .spaces[0].metrics.halstead.purity_ratio
old: 0.964925569107265
new: 1.4932837829515606

path: .spaces[0].metrics.halstead.vocabulary
old: 38.0
new: 134.0

path: .spaces[0].metrics.halstead.volume
old: 881.6518222585223
new: 4119.529998036881

path: .spaces[0].metrics.nom.functions
old: 1.0
new: 31.0

path: .spaces[0].metrics.nom.total
old: 1.0
new: 31.0

path: .spaces[0].metrics.nexits.average
old: 0.0
new: 0.967741935483871

path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 30.0

path: .spaces[0].metrics.nargs.average
old: 5.0
new: 0.2903225806451613

path: .spaces[0].metrics.nargs.sum
old: 5.0
new: 9.0

path: .spaces[0].metrics.loc.cloc
old: 0.0
new: 39.0

path: .spaces[0].metrics.loc.sloc
old: 45.0
new: 159.0

path: .spaces[0].metrics.loc.lloc
old: 20.0
new: 30.0

path: .spaces[0].metrics.loc.ploc
old: 45.0
new: 124.0

Code

namespace mozilla {

enum class VMFlag : uint8_t {
  Readable,       // rd  - readable
  Writable,       // wr  - writable
  Executable,     // ex  - executable
  Shared,         // sh  - shared
  MayRead,        // mr  - may read
  MayWrite,       // mw  - may write
  MayExecute,     // me  - may execute
  MayShare,       // ms  - may share
  GrowsDown,      // gd  - stack segment grows down
  PurePFN,        // pf  - pure PFN range
  DisabledWrite,  // dw  - disabled write to the mapped file
  Locked,         // lo  - pages are locked in memory
  IO,             // io  - memory mapped I/O area
  Sequential,     // sr  - sequential read advise provided
  Random,         // rr  - random read advise provided
  NoFork,         // dc  - do not copy area on fork
  NoExpand,       // de  - do not expand area on remapping
  Accountable,    // ac  - area is accountable
  NotReserved,    // nr  - swap space is not reserved for the area
  HugeTLB,        // ht  - area uses huge tlb pages
  NonLinear,      // nl  - non-linear mapping
  ArchSpecific,   // ar  - architecture specific flag
  NoCore,         // dd  - do not include area into core dump
  SoftDirty,      // sd  - soft-dirty flag
  MixedMap,       // mm  - mixed map area
  HugePage,       // hg  - huge page advise flag
  NoHugePage,     // nh  - no-huge page advise flag
  Mergeable,      // mg  - mergeable advise flag
};

using VMFlagSet = EnumSet;

class MemoryMapping final {
 public:
  enum class Perm : uint8_t {
    Read,
    Write,
    Execute,
    Shared,
    Private,
  };

  using PermSet = EnumSet;

  MemoryMapping(uintptr_t aStart, uintptr_t aEnd, PermSet aPerms,
                size_t aOffset, const char* aName)
      : mStart(aStart),
        mEnd(aEnd),
        mOffset(aOffset),
        mName(aName),
        mPerms(aPerms) {}

  const nsCString& Name() const { return mName; }

  uintptr_t Start() const { return mStart; }
  uintptr_t End() const { return mEnd; }

  bool Includes(const void* aPtr) const {
    auto ptr = uintptr_t(aPtr);
    return ptr >= mStart && ptr < mEnd;
  }

  PermSet Perms() const { return mPerms; }
  VMFlagSet VMFlags() const { return mFlags; }

  // For file mappings, the offset in the mapped file which corresponds to the
  // start of the mapped region.
  size_t Offset() const { return mOffset; }

  size_t AnonHugePages() const { return mAnonHugePages; }
  size_t Anonymous() const { return mAnonymous; }
  size_t KernelPageSize() const { return mKernelPageSize; }
  size_t LazyFree() const { return mLazyFree; }
  size_t Locked() const { return mLocked; }
  size_t MMUPageSize() const { return mMMUPageSize; }
  size_t Private_Clean() const { return mPrivate_Clean; }
  size_t Private_Dirty() const { return mPrivate_Dirty; }
  size_t Private_Hugetlb() const { return mPrivate_Hugetlb; }
  size_t Pss() const { return mPss; }
  size_t Referenced() const { return mReferenced; }
  size_t Rss() const { return mRss; }
  size_t Shared_Clean() const { return mShared_Clean; }
  size_t Shared_Dirty() const { return mShared_Dirty; }
  size_t Shared_Hugetlb() const { return mShared_Hugetlb; }
  size_t ShmemPmdMapped() const { return mShmemPmdMapped; }
  size_t Size() const { return mSize; }
  size_t Swap() const { return mSwap; }
  size_t SwapPss() const { return mSwapPss; }

  // Dumps a string representation of the entry, similar to its format in the
  // smaps file, to the given string. Mainly useful for debugging.
  void Dump(nsACString& aOut) const;

  // These comparison operators are used for binary searching sorted arrays of
  // MemoryMapping entries to find the one which contains a given pointer.
  bool operator==(const void* aPtr) const { return Includes(aPtr); }
  bool operator<(const void* aPtr) const { return mStart < uintptr_t(aPtr); }

 private:
  friend nsresult GetMemoryMappings(nsTArray& aMappings,
                                    pid_t aPid);

  uintptr_t mStart = 0;
  uintptr_t mEnd = 0;

  size_t mOffset = 0;

  nsCString mName;

  // Members for size fields in the smaps file. Please keep these in sync with
  // the sFields array.
  size_t mAnonHugePages = 0;
  size_t mAnonymous = 0;
  size_t mKernelPageSize = 0;
  size_t mLazyFree = 0;
  size_t mLocked = 0;
  size_t mMMUPageSize = 0;
  size_t mPrivate_Clean = 0;
  size_t mPrivate_Dirty = 0;
  size_t mPrivate_Hugetlb = 0;
  size_t mPss = 0;
  size_t mReferenced = 0;
  size_t mRss = 0;
  size_t mShared_Clean = 0;
  size_t mShared_Dirty = 0;
  size_t mShared_Hugetlb = 0;
  size_t mShmemPmdMapped = 0;
  size_t mSize = 0;
  size_t mSwap = 0;
  size_t mSwapPss = 0;

  PermSet mPerms{};
  VMFlagSet mFlags{};

  // Contains the name and offset of one of the above size_t fields, for use in
  // parsing in dumping. The below helpers contain a list of the fields, and map
  // Field entries to the appropriate member in a class instance.
  struct Field {
    const char* mName;
    size_t mOffset;
  };

  static const Field sFields[20];

  size_t& ValueForField(const Field& aField) {
    char* fieldPtr = reinterpret_cast(this) + aField.mOffset;
    return reinterpret_cast(fieldPtr)[0];
  }
  size_t ValueForField(const Field& aField) const {
    return const_cast(this)->ValueForField(aField);
  }
};

nsresult GetMemoryMappings(nsTArray& aMappings, pid_t aPid = 0);

}  // namespace mozilla