PpmSnapPerformanceAccumulation

INT64 __fastcall PpmSnapPerformanceAccumulation(
        _KPRCB *Prcb,
        UINT64 Reset,
        INT64 Remote,
        INT64 SnapPmc,
        _PROC_PERF_CHECK_SNAP *Snap){
  INT64 *v5; 
  char v6; 
  unsigned __int8 v7; 
  unsigned __int64 v9; 
  ULONG_PTR v10; 
  __int64 v11; 
  __int64 v12; 
  volatile unsigned __int64 v13; 
  UINT64 v14; 
  INT64 result; 
  INT64 v16; 
  ULONG_PTR v17; 
  __int64 v18; 
  __int64 v19; 
  volatile unsigned __int64 QpcBias; 
  unsigned __int64 v21; 
  unsigned __int64 v22; 
  unsigned __int64 v23; 
  unsigned __int64 v24; 
  __int64 v25; 
  INT64 v26; 
  unsigned __int64 v27; 
  unsigned __int64 v28; 
  __int64 v29; 
  unsigned __int64 v30; 
  INT64 v31; 
  __int64 v32; 
  UINT64 v33; 
  __int64 v34; 
  __int64 v35; 
  __int64 v36; 
  unsigned __int64 v37; 
  __int64 v38; 
  int v39; 
  unsigned __int64 v40; 
  __int64 v41; 
  __int64 v42; 
  unsigned __int64 v43; 
  signed __int64 v44; 
  __int64 v45; 
  bool v46; 
  __int64 v47; 
  UINT64 v48; 
  __int64 v49; 
  __int64 v50; 
  unsigned __int64 v51; 
  __int64 v52; 
  int v53; 
  unsigned __int64 v54; 
  __int64 v55; 
  __int64 v56; 
  unsigned __int64 v57; 
  signed __int64 v58; 
  __int64 v59; 
  UINT64 v60; 
  __int64 v61; 
  UINT64 v62; 
  __int64 v63; 
  __int64 v64; 
  __int64 v65; 
  unsigned __int64 v66; 
  __int64 v67; 
  int v68; 
  unsigned __int64 v69; 
  __int64 v70; 
  __int64 v71; 
  unsigned __int64 v72; 
  signed __int64 v73; 
  __int64 v74; 
  __int64 v75; 
  UINT64 v76; 
  __int64 v77; 
  __int64 v78; 
  unsigned __int64 v79; 
  __int64 v80; 
  int v81; 
  unsigned __int64 v82; 
  __int64 v83; 
  __int64 v84; 
  unsigned __int64 v85; 
  signed __int64 v86; 
  __int64 v87; 
  UINT64 v88; 
  signed __int32 v89[8]; 
  char v90; 
  UINT8 v91; 
  UINT64 FromFrequency; 
  UINT64 v93; 
  __int64 v94; 
  __int128 v95; 
  __int64 v96; 
  v5 = 0i64;
  v90 = SnapPmc;
  v91 = Reset;
  v96 = 0i64;
  v6 = SnapPmc;
  v94 = 0i64;
  v7 = Remote;
  v9 = 0i64;
  v95 = 0i64;
  if( !(_BYTE)Remote )
  {
    _disable();
    v17 = HalpPerformanceCounter;
    if( *(_DWORD *)(HalpPerformanceCounter + 228) == 5 )
    {
      v93 = 10000000i64;
      if( !HalpTimerReferencePage )
      {
        if( *(_DWORD *)(HalpPerformanceCounter + 220) == 64 )
        {
          HalpTimerGetInternalData((KDPC *)HalpPerformanceCounter, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
          v62 = (*(__int64(__fastcall **)(__int64))(v17 + 112))(v61);
          v63 = *(_QWORD *)(v17 + 208);
          Reset = v62;
        }
        else
        {
          do
          {
            v63 = *(_QWORD *)(v17 + 208);
            do
            {
              v64 = *(_QWORD *)(v17 + 200);
              HalpTimerGetInternalData((KDPC *)v17, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
              v66 = (*(__int64(__fastcall **)(__int64))(v17 + 112))(v65);
              _InterlockedOr(v89, 0);
              v67 = *(_QWORD *)(v17 + 200);
            }
            while( v64 != v67 );
          }
          while( v63 != *(_QWORD *)(v17 + 208) );
          v68 = *(_DWORD *)(v17 + 220);
          v69 = v64 ^ v66;
          if( _bittest64((const __int64 *)&v69, (unsigned __int8)(v68 - 1)) )
          {
            v70 = 1i64;
            if( v68 == 64 )
              v71 = -1i64;
            else
              v71 = (1i64 << v68) - 1;
            if( v68 != 64 )
              v70 = 1i64 << v68;
            v72 = v64 & v71;
            Reset = v66 | v64 ^ v72;
            if( v66 < v72 )
              Reset += v70;
            _InterlockedCompareExchange64((volatile signed __int64 *)(v17 + 200), Reset, v73);
          }
          else
          {
            if( v68 == 64 )
              v74 = -1i64;
            else
              v74 = (1i64 << v68) - 1;
            Reset = v66 | v64 & ~v74;
          }
        }
        v14 = HalpTimerScaleCounter(v63 + Reset, *(_QWORD *)(v17 + 192), 0x989680ui64);
        goto LABEL_21;
      }
      if( (*(_DWORD *)(HalpPerformanceCounter + 224) & 0x10000) != 0 )
        v18 = *(_QWORD *)(HalpPerformanceCounter + 72)
            + (unsigned int)(*(_DWORD *)(HalpPerformanceCounter + 80) * *((_DWORD *)KeGetPcr() + 105));
      else
        v18 = *(_QWORD *)(HalpPerformanceCounter + 72);
      v19 = (*(__int64(__fastcall **)(__int64))(HalpPerformanceCounter + 112))(v18);
      QpcBias = KUSER_SHARED_DATA.QpcBias;
      *(_OWORD *)(&Reset - 1) = (unsigned __int64)v19
                              * (unsigned __int128)*((unsigned __int64 *)HalpTimerReferencePage + 1);
    }
    else
    {
      v46 = *(_DWORD *)(HalpPerformanceCounter + 220) == 64;
      v93 = *(_QWORD *)(HalpPerformanceCounter + 192);
      if( v46 )
      {
        HalpTimerGetInternalData((KDPC *)HalpPerformanceCounter, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
        v76 = (*(__int64(__fastcall **)(__int64))(v17 + 112))(v75);
        QpcBias = *(_QWORD *)(v17 + 208);
        Reset = v76;
      }
      else
      {
        do
        {
          QpcBias = *(_QWORD *)(v17 + 208);
          do
          {
            v77 = *(_QWORD *)(v17 + 200);
            HalpTimerGetInternalData((KDPC *)v17, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
            v79 = (*(__int64(__fastcall **)(__int64))(v17 + 112))(v78);
            _InterlockedOr(v89, 0);
            v80 = *(_QWORD *)(v17 + 200);
          }
          while( v77 != v80 );
        }
        while( QpcBias != *(_QWORD *)(v17 + 208) );
        v81 = *(_DWORD *)(v17 + 220);
        v82 = v77 ^ v79;
        if( _bittest64((const __int64 *)&v82, (unsigned __int8)(v81 - 1)) )
        {
          v83 = 1i64;
          if( v81 == 64 )
            v84 = -1i64;
          else
            v84 = (1i64 << v81) - 1;
          if( v81 != 64 )
            v83 = 1i64 << v81;
          v85 = v77 & v84;
          Reset = v79 | v77 ^ v85;
          if( v79 < v85 )
            Reset += v83;
          _InterlockedCompareExchange64((volatile signed __int64 *)(v17 + 200), Reset, v86);
        }
        else
        {
          if( v81 == 64 )
            v87 = -1i64;
          else
            v87 = (1i64 << v81) - 1;
          Reset = v79 | v77 & ~v87;
        }
      }
    }
    v14 = Reset + QpcBias;
LABEL_21:
    if( v17 != HalpOriginalPerformanceCounter && HalpOriginalPerformanceCounter )
    {
      v88 = *(_QWORD *)(HalpOriginalPerformanceCounter + 192);
      if( *(_DWORD *)(HalpOriginalPerformanceCounter + 228) == 5 )
        v88 = 10000000i64;
      v14 = HalpTimerScaleCounter(v14, v93, v88);
    }
    v21 = __rdtsc();
    if( (*((_QWORD *)Prcb + 4276) & 0x8000000000i64) != 0 )
      v22 = __readmsr(0xDB2u);
    else
      v22 = 0i64;
    v23 = v14 - *((_QWORD *)Prcb + 4110);
    _InterlockedExchangeAdd64((volatile signed __int64 *)Prcb + 4112, v23);
    if( (*((_QWORD *)Prcb + 4276) & 0x8000000000i64) != 0 )
      *((_QWORD *)Prcb + 4121) += PpmConvertTime(v23, v21 - *((_QWORD *)Prcb + 4106), v22 - *((_QWORD *)Prcb + 4120));
    v24 = *((_QWORD *)Prcb + 4106);
    *((_QWORD *)Prcb + 4110) = v14;
    if( v21 > v24 )
      *((_QWORD *)Prcb + 4107) += v21 - v24;
    *((_QWORD *)Prcb + 4106) = v21;
    v5 = 0i64;
    if( (*((_QWORD *)Prcb + 4276) & 0x8000000000i64) != 0 )
      *((_QWORD *)Prcb + 4120) = v22;
    v6 = v90;
    goto LABEL_31;
  }
  v10 = HalpPerformanceCounter;
  if( *(_DWORD *)(HalpPerformanceCounter + 228) != 5 )
  {
    v46 = *(_DWORD *)(HalpPerformanceCounter + 220) == 64;
    FromFrequency = *(_QWORD *)(HalpPerformanceCounter + 192);
    if( v46 )
    {
      HalpTimerGetInternalData((KDPC *)HalpPerformanceCounter, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
      v48 = (*(__int64(__fastcall **)(__int64))(v10 + 112))(v47);
      v13 = *(_QWORD *)(v10 + 208);
      Reset = v48;
    }
    else
    {
      do
      {
        v13 = *(_QWORD *)(v10 + 208);
        do
        {
          v49 = *(_QWORD *)(v10 + 200);
          HalpTimerGetInternalData((KDPC *)v10, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
          v51 = (*(__int64(__fastcall **)(__int64))(v10 + 112))(v50);
          _InterlockedOr(v89, 0);
          v52 = *(_QWORD *)(v10 + 200);
        }
        while( v49 != v52 );
      }
      while( v13 != *(_QWORD *)(v10 + 208) );
      v53 = *(_DWORD *)(v10 + 220);
      v54 = v49 ^ v51;
      if( _bittest64((const __int64 *)&v54, (unsigned __int8)(v53 - 1)) )
      {
        v55 = 1i64;
        if( v53 == 64 )
          v56 = -1i64;
        else
          v56 = (1i64 << v53) - 1;
        if( v53 != 64 )
          v55 = 1i64 << v53;
        v57 = v49 & v56;
        Reset = v51 | v49 ^ v57;
        if( v51 < v57 )
          Reset += v55;
        _InterlockedCompareExchange64((volatile signed __int64 *)(v10 + 200), Reset, v58);
        v5 = 0i64;
      }
      else
      {
        if( v53 == 64 )
          v59 = -1i64;
        else
          v59 = (1i64 << v53) - 1;
        Reset = v51 | v49 & ~v59;
        v5 = 0i64;
      }
    }
    goto LABEL_7;
  }
  FromFrequency = 10000000i64;
  if( HalpTimerReferencePage )
  {
    if( (*(_DWORD *)(HalpPerformanceCounter + 224) & 0x10000) != 0 )
      v11 = *(_QWORD *)(HalpPerformanceCounter + 72)
          + (unsigned int)(*(_DWORD *)(HalpPerformanceCounter + 80) * *((_DWORD *)KeGetPcr() + 105));
    else
      v11 = *(_QWORD *)(HalpPerformanceCounter + 72);
    v12 = (*(__int64(__fastcall **)(__int64))(HalpPerformanceCounter + 112))(v11);
    v13 = KUSER_SHARED_DATA.QpcBias;
    *(_OWORD *)(&Reset - 1) = (unsigned __int64)v12
                            * (unsigned __int128)*((unsigned __int64 *)HalpTimerReferencePage + 1);
LABEL_7:
    v14 = Reset + v13;
    goto LABEL_8;
  }
  if( *(_DWORD *)(HalpPerformanceCounter + 220) == 64 )
  {
    HalpTimerGetInternalData((KDPC *)HalpPerformanceCounter, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
    v33 = (*(__int64(__fastcall **)(__int64))(v10 + 112))(v32);
    v34 = *(_QWORD *)(v10 + 208);
    Reset = v33;
  }
  else
  {
    do
    {
      v34 = *(_QWORD *)(v10 + 208);
      do
      {
        v35 = *(_QWORD *)(v10 + 200);
        HalpTimerGetInternalData((KDPC *)v10, (PVOID)Reset, (PVOID)Remote, (PVOID)SnapPmc);
        v37 = (*(__int64(__fastcall **)(__int64))(v10 + 112))(v36);
        _InterlockedOr(v89, 0);
        v38 = *(_QWORD *)(v10 + 200);
      }
      while( v35 != v38 );
    }
    while( v34 != *(_QWORD *)(v10 + 208) );
    v39 = *(_DWORD *)(v10 + 220);
    v40 = v35 ^ v37;
    if( _bittest64((const __int64 *)&v40, (unsigned __int8)(v39 - 1)) )
    {
      v41 = 1i64;
      if( v39 == 64 )
        v42 = -1i64;
      else
        v42 = (1i64 << v39) - 1;
      if( v39 != 64 )
        v41 = 1i64 << v39;
      v43 = v35 & v42;
      Reset = v37 | v35 ^ v43;
      if( v37 < v43 )
        Reset += v41;
      _InterlockedCompareExchange64((volatile signed __int64 *)(v10 + 200), Reset, v44);
      v5 = 0i64;
    }
    else
    {
      if( v39 == 64 )
        v45 = -1i64;
      else
        v45 = (1i64 << v39) - 1;
      Reset = v37 | v35 & ~v45;
      v5 = 0i64;
    }
  }
  v14 = HalpTimerScaleCounter(v34 + Reset, *(_QWORD *)(v10 + 192), 0x989680ui64);
LABEL_8:
  if( v10 != HalpOriginalPerformanceCounter && HalpOriginalPerformanceCounter )
  {
    v60 = *(_QWORD *)(HalpOriginalPerformanceCounter + 192);
    if( *(_DWORD *)(HalpOriginalPerformanceCounter + 228) == 5 )
      v60 = 10000000i64;
    v14 = HalpTimerScaleCounter(v14, FromFrequency, v60);
  }
  _m_prefetchw((char *)Prcb + 32800);
  result = *((_QWORD *)Prcb + 4100);
  do
  {
    v16 = result;
    result = _InterlockedCompareExchange64((volatile signed __int64 *)Prcb + 4100, result, result);
  }
  while( v16 != result );
  v9 = result;
  if( result )
  {
    if( *((_DWORD *)Prcb + 8246) == 3 )
    {
      result = *((_QWORD *)Prcb + 4202);
      v9 = *(_QWORD *)(result + 8i64 * DWORD2(xmmword_140CED0E0)) >> 1;
      if( (*(_QWORD *)(result + 8i64 * DWORD2(xmmword_140CED0E0)) & 1) == 0 )
        v9 = 0i64;
    }
  }
  if( !v9 )
    goto LABEL_14;
LABEL_31:
  memset((INT64)Snap, 0i64);
  Snap->CyclesAffinitized = *((_QWORD *)Prcb + 4061) + *((_QWORD *)Prcb + 4071);
  Snap->TaggedThreadCycles[0] = *((_QWORD *)Prcb + 4058);
  Snap->TaggedThreadCycles[1] = *((_QWORD *)Prcb + 4059);
  Snap->TaggedThreadCycles[2] = *((_QWORD *)Prcb + 4060);
  if( !v7 )
    _enable();
  if( *((_DWORD *)Prcb + 8246) == 3 )
    v5 = (INT64 *)&v95;
  LOBYTE(result) = PpmUpdatePerformanceFeedback((INT64)Prcb, v91, v7, 0, v5);
  if( (_BYTE)result )
  {
    if( v6 && PopSnapEnergyCounters )
    {
      LOBYTE(v25) = 1;
      PopSnapEnergyCounters(*((unsigned int *)Prcb + 9), v7, v25);
    }
    if( v5 )
    {
      v28 = *v5;
      v26 = v5[1];
      v27 = v5[2];
      v29 = v94;
    }
    else
    {
      v26 = *((_QWORD *)Prcb + 4099);
      v27 = *((_QWORD *)Prcb + 4107);
      v28 = v9;
      if( v14 > v9 )
        v28 = v14;
      v29 = v9;
    }
    if( *((_QWORD *)Prcb + 4108) )
    {
      v30 = *((_QWORD *)Prcb + 4113);
    }
    else if( *((_QWORD *)Prcb + 4109) )
    {
      v30 = *((_QWORD *)Prcb + 4114);
    }
    else
    {
      v30 = 100i64 * *((_QWORD *)Prcb + 4111);
    }
    Snap->FrequencyScaledActive = v30;
    if( *((_QWORD *)Prcb + 4109) )
      v30 = *((_QWORD *)Prcb + 4114);
    Snap->PerformanceScaledActive = v30;
    Snap->PerformanceScaledKernelActive = *((_QWORD *)Prcb + 4116);
    Snap->Stall = *((_QWORD *)Prcb + 4121);
    result = *((unsigned int *)Prcb + 3127);
    Snap->ResponsivenessEvents = result;
    if( v7 )
    {
      _m_prefetchw((char *)Prcb + 32800);
      result = *((_QWORD *)Prcb + 4100);
      do
      {
        v31 = result;
        result = _InterlockedCompareExchange64((volatile signed __int64 *)Prcb + 4100, result, result);
      }
      while( v31 != result );
      if( result )
      {
        if( *((_DWORD *)Prcb + 8246) == 3 )
        {
          result = *(_QWORD *)(*((_QWORD *)Prcb + 4202) + 8i64 * DWORD2(xmmword_140CED0E0)) >> 1;
          if( (*(_QWORD *)(*((_QWORD *)Prcb + 4202) + 8i64 * DWORD2(xmmword_140CED0E0)) & 1) == 0 )
            result = 0i64;
        }
      }
      if( v9 != result )
      {
LABEL_14:
        LOBYTE(result) = 0;
        return result;
      }
      if( !v5 )
      {
        result = v28 - v29;
        v26 += v28 - v29;
      }
    }
    else if( !v5 )
    {
      v26 += *((_QWORD *)Prcb + 4098);
    }
    Snap->Time = v28;
    LOBYTE(result) = 1;
    Snap->CyclesActive = v27;
    Snap->Active = v28 - v26;
  }
  return result;
}

Referenced by:

PpmCapturePerformanceDistributionCallback
PpmCheckSnapAllDeliveredPerformance
PpmGetThroughputInfoCallback
PpmPerfSnapDeliveredPerformance
PpmResetPerfTimes