Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1.  
  2. #include "globals.h"
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <intrin.h>
  8. #include <string>
  9. #include <windows.h>
  10.  
  11. #include <algorithm>
  12. #include <map>
  13. #include <unordered_map>
  14. using namespace std;
  15.  
  16. namespace perf_map_ {
  17.     typedef unsigned long long uint64;
  18.  
  19.  
  20. #define SIZE_OF_STAT 10
  21. #define BOUND_OF_LOOP 10
  22.  
  23.  
  24.  
  25.     const int core = 2;
  26.     void processor() {
  27.  
  28.         int mask = 1 << core;
  29.  
  30.         DWORD_PTR pa, sa;
  31.         GetProcessAffinityMask( GetCurrentProcess(), &pa, &sa );
  32.  
  33.         if( !(mask & pa) ) {
  34.             puts( "error0" ); printf( "%d %d\n", mask, pa ); system( "PAUSE" ); return;
  35.         }
  36.  
  37.         //
  38.         if( !SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS ) ) {
  39.             puts( "error1a" ); system( "PAUSE" ); return;
  40.         }
  41.  
  42.         if( !SetProcessAffinityMask( GetCurrentProcess(), mask ) ) {
  43.             puts( "error1b" ); system( "PAUSE" ); return;
  44.         }
  45.  
  46.         if( !SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL ) ) {
  47.             puts( "error2a" ); system( "PAUSE" ); return;
  48.         }
  49.  
  50.         if( !SetThreadAffinityMask( GetCurrentThread(), mask ) ) {
  51.             puts( "error2b" ); system( "PAUSE" ); return;
  52.         }
  53.  
  54.         Sleep( 1 );
  55.  
  56.         printf( "core %d\n", GetCurrentProcessorNumber() );
  57.     }
  58.  
  59.  
  60.  
  61. #define _CYCLES_START \
  62. __asm   CPUID \
  63. __asm   RDTSC \
  64. __asm   mov cycles_high, edx \
  65. __asm   mov cycles_low, eax \
  66.  
  67. #define _CYCLES_END \
  68. __asm   RDTSCP \
  69. __asm   mov cycles_high1, edx \
  70. __asm   mov cycles_low1, eax \
  71. __asm   CPUID \
  72.  
  73.  
  74.  
  75.  
  76.     std::map<int, string> map_obj;
  77.     std::unordered_map<int, string> umap_obj;
  78.  
  79.     const int N = 10;
  80.  
  81.     void test_init()
  82.     {
  83.         for( int k=0; k < N; ++k ) {
  84.             //int x = i + (100 * k);
  85.             map_obj[k] = "aha";// to_string( x );
  86.         }
  87.  
  88.         for( int k=0; k < N; ++k ) {
  89.             //int x = i + (100 * k);
  90.             umap_obj[k] = "aha";//to_string( x );
  91.         }
  92.     }
  93.  
  94.     void test_destroy() {
  95.         map_obj.clear();
  96.         umap_obj.clear();
  97.     }
  98.  
  99.     //
  100.  
  101.     void test_map( int i ) {
  102.         for( int k=0; k < 10; ++k )
  103.             string x = map_obj[k];
  104.     }
  105.  
  106.     void test_unordered_map( int i ) {
  107.         for( int k=0; k < 10; ++k )
  108.             string x = umap_obj[k];
  109.     }
  110.  
  111.  
  112.  
  113.  
  114.     void inline Filltimes( uint64 **times, void( *pre_func )(), void( *func )(int), void( *post_func )() ) {
  115.         unsigned long flags;
  116.         int i, j;
  117.         uint64 start, end;
  118.         unsigned cycles_low, cycles_high=0, cycles_low1, cycles_high1=0;
  119.         volatile int variable = 0;
  120.  
  121.         pre_func();
  122.         func( 0 );
  123.         post_func();
  124.  
  125.         _CYCLES_START
  126.             _CYCLES_END
  127.  
  128.             pre_func();
  129.         func( 0 );
  130.         post_func();
  131.  
  132.         _CYCLES_START
  133.             _CYCLES_END
  134.  
  135.  
  136.             _CYCLES_START
  137.             _CYCLES_END
  138.             start = (((uint64)cycles_high << 32) | cycles_low);
  139.         end = (((uint64)cycles_high1 << 32) | cycles_low1);
  140.         uint64 diff = end - start;
  141.  
  142.         for( j=0; j < BOUND_OF_LOOP; j++ ) {
  143.             for( i =0; i < SIZE_OF_STAT; i++ ) {
  144.                 variable = 0;
  145.  
  146.                 pre_func();
  147.  
  148.                 _CYCLES_START
  149.  
  150.                     func( i );
  151.  
  152.                 _CYCLES_END
  153.  
  154.                     post_func();
  155.  
  156.  
  157.                 start = (((uint64)cycles_high << 32) | cycles_low);
  158.  
  159.                 end = (((uint64)cycles_high1 << 32) | cycles_low1);
  160.  
  161.                 if( diff + start > end ) times[j][i]=1;
  162.                 else times[j][i] = end - start;// -diff;
  163.             }
  164.         }
  165.     }
  166.  
  167.  
  168.     uint64 var_calc( uint64 *inputs, int size )
  169.     {
  170.         int i;
  171.         uint64 acc = 0, previous = 0, temp_var = 0;
  172.         for( i=0; i < size; i++ ) {
  173.             if( acc < previous ) goto overflow;
  174.             previous = acc;
  175.             acc += inputs[i];
  176.         }
  177.         acc = acc * acc;
  178.         if( acc < previous ) goto overflow;
  179.         previous = 0;
  180.         for( i=0; i < size; i++ ) {
  181.             if( temp_var < previous ) goto overflow;
  182.             previous = temp_var;
  183.             temp_var+= (inputs[i] * inputs[i]);
  184.         }
  185.         temp_var = temp_var * size;
  186.         if( temp_var < previous ) goto overflow;
  187.         temp_var =(temp_var - acc) / (((uint64)(size))*((uint64)(size)));
  188.         return (temp_var);
  189.     overflow:
  190.         return -EINVAL;
  191.     }
  192.  
  193.  
  194.     uint64 ** init_times()
  195.     {
  196.         uint64 **times;
  197.         int j = 0, k = 0;
  198.  
  199.         times = (uint64 **)malloc( BOUND_OF_LOOP * sizeof( uint64* ) );
  200.         if( !times ) {
  201.             return nullptr;
  202.         }
  203.  
  204.         for( j=0; j < BOUND_OF_LOOP; j++ ) {
  205.             times[j] = (uint64 *)malloc( SIZE_OF_STAT * sizeof( uint64 ) );
  206.             if( !times[j] ) {
  207.                 for( k=0; k < j; k++ )
  208.                     free( times[k] );
  209.                 return nullptr;
  210.             }
  211.         }
  212.         return times;
  213.     }
  214.  
  215.     void do_test_set( void( *pre_func )(), void( *func )(int), void( *post_func )() ) {
  216.         int i = 0, j = 0, spurious = 0, k =0;
  217.         uint64 **times;
  218.         uint64 *variances;
  219.         uint64 *min_values;
  220.         uint64 max_dev = 0, min_time = 0, max_time = 0, prev_min =0, tot_var=0, max_dev_all=0, var_of_vars=0, var_of_mins=0;
  221.  
  222.         times= init_times();
  223.         if( !times ) return;
  224.  
  225.         variances = (uint64 *)malloc( BOUND_OF_LOOP * sizeof( uint64 ) );
  226.         if( !variances )  return;
  227.  
  228.         min_values = (uint64 *)malloc( BOUND_OF_LOOP * sizeof( uint64 ) );
  229.         if( !min_values )  return;
  230.  
  231.  
  232.         Filltimes( times, pre_func, func, post_func );
  233.  
  234.  
  235.         for( j=0; j < BOUND_OF_LOOP; j++ )
  236.         {
  237.             max_dev = 0;
  238.             min_time = 0;
  239.             max_time = 0;
  240.  
  241.             for( i =0; i < SIZE_OF_STAT; i++ ) {
  242.                 if( (min_time == 0) || (min_time > times[j][i]) )
  243.                     min_time = times[j][i];
  244.                 if( max_time < times[j][i] )
  245.                     max_time = times[j][i];
  246.             }
  247.  
  248.             max_dev = max_time - min_time;
  249.             min_values[j] = min_time;
  250.  
  251.             if( (prev_min != 0) && (prev_min > min_time) )
  252.                 spurious++;
  253.             if( max_dev > max_dev_all )
  254.                 max_dev_all = max_dev;
  255.  
  256.             variances[j] = var_calc( times[j], SIZE_OF_STAT );
  257.             tot_var += variances[j];
  258.  
  259.             printf( "\nj:%d >> variance: %I64u; max_deviation: %I64u ;min time: %I64u", j, variances[j], max_dev, min_time );
  260.  
  261.             prev_min = min_time;
  262.         }
  263.  
  264.         var_of_vars = var_calc( variances, BOUND_OF_LOOP );
  265.         var_of_mins = var_calc( min_values, BOUND_OF_LOOP );
  266.  
  267.         printf( "\n total number of spurious min values = %d", spurious );
  268.         printf( "\n total variance = %I64u", (tot_var / BOUND_OF_LOOP) );
  269.         printf( "\n absolute max deviation = %I64u", max_dev_all );
  270.         printf( "\n variance of variances = %I64u", var_of_vars );
  271.         printf( "\n variance of minimum values = %I64u", var_of_mins );
  272.         printf( "\n" );
  273.  
  274.         for( j=0; j < BOUND_OF_LOOP; j++ ) {
  275.             free( times[j] );
  276.         }
  277.         free( times );
  278.         free( variances );
  279.         free( min_values );
  280.     }
  281.  
  282.  
  283.     void test()
  284.     {
  285.         processor();
  286.  
  287.         do_test_set( test_init, test_map, test_destroy );
  288.         do_test_set( test_init, test_unordered_map, test_destroy );
  289.     }
  290.  
  291. };
  292.  
  293. void perf_map() {
  294.     perf_map_::test();
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement