Exemple #1
0
        public ThreadSamplingHistogramForm()
        {
            InitializeComponent();
            ChartSize = 250;

            m_sampleHistogrammDataFactory = new SampleHistogrammDataFactory(ChartSize);
        }
Exemple #2
0
        protected void ThreadFunc()
        {
            m_stopped = false;
            State     = ThreadState.Started;

            SetAfinnityMask();

            Thread.BeginCriticalRegion();
            int lastError;

            do
            {
                NativeMethods.SetThreadPriority(m_threadHandle, m_priority);
                lastError = Marshal.GetLastWin32Error();
                if (lastError != 0)
                {
                    LastErrorMessage = new Win32Exception(lastError).Message;
                }
                else
                {
                    LastErrorMessage = string.Empty;
                }

                m_looper = true;
                Thread.MemoryBarrier();
                m_restartLoop = false;

                switch (m_loadType)
                {
                case LoadType.YieldExecution:
                    while (m_looper)
                    {
                        NativeMethods.SwitchToThread();
                        ++m_looped;
                        Thread.MemoryBarrier();
                    }
                    break;

                case LoadType.Sleep:
                    while (m_looper)
                    {
                        Thread.Sleep(0);

                        ++m_looped;
                        Thread.MemoryBarrier();
                    }
                    break;

                case LoadType.EmptyLoop:
                    while (m_looper)
                    {
                        ++m_looped;
                        Thread.MemoryBarrier();
                    }
                    break;

                case LoadType.PrepareSampleHistogramm50k:
                {
                    SampleHistogrammDataFactory dataFactory = new SampleHistogrammDataFactory(500);
                    while (m_looper)
                    {
                        ++m_looped;
                        Thread.MemoryBarrier();

                        var sortedUniques    = dataFactory.GetSortedUniques(TestData.TestDoubles50k);
                        var histagrammValues = dataFactory.GetHistagrammValues(sortedUniques);
                        GC.KeepAlive(histagrammValues);
                    }
                }
                break;

                case LoadType.SpinWait:
                    // while (m_looper.Value) PAUSE
                    //not implemented Interlocked.Increment(ref m_looped);
                    unsafe
                    {
                        fixed(void *v = &m_looper)
                        {
                            s_pause(0, (byte *)v);
                        }
                    }

                    break;

                case LoadType.MemoryPressure:
                    while (m_looper)
                    {
                        MemoryPressureFunction();
                        {
                            ++m_looped;
                            Thread.MemoryBarrier();
                        }
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            } while (m_restartLoop);

            State = ThreadState.Stopped;
            Thread.EndCriticalRegion();

            NativeMethods.CloseHandle(m_threadHandle);
            lastError = Marshal.GetLastWin32Error();
            if (lastError != 0)
            {
                LastErrorMessage = new Win32Exception(lastError).Message;
            }
            else
            {
                LastErrorMessage = string.Empty;
            }

            Reinit();
        }