Search code examples
c#asp.netperformanceunsafesystem.diagnostics

using unsafe code in C# asp.net


trying to debug a modified code to be using unsafe generates an error i dont know if my code is correct with the astrics (so please check if i put those correctly) though there is another issue apart from that... with compiler options in asp.net as opposed to Windows apps, there's no sutible place to check/uncheck the option to using unsafe

and as far as i could get to info about that envolves dealing with the Web.Config "manually"

so i was doing that , added a code to Web.Config, there are two versions of users suggesting to put code within configuration section or within the same scope of debug=true ...

so i put it in both(not at same time , but tried both (:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
 <system.codedom>
   <compilers>
     <compiler language="C#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </compilers>
  </system.codedom>
  <connectionStrings>....my secret connection here...
   then rest of configs..
     .....




    struct IO_COUNTERS
    {
        public ulong ReadOperationCount;
        public ulong WriteOperationCount;
        public ulong OtherOperationCount;
        public ulong ReadTransferCount;
        public ulong WriteTransferCount;
        public ulong OtherTransferCount;
    }
    [DllImport("kernel32.dll")]
    unsafe static extern bool GetProcessIoCounters(IntPtr* ProcessHandle, out IO_COUNTERS* IoCounters);


    private struct PROCESS_MEMORY_COUNTERS
    {
        public uint cb;
        public uint PageFaultCount;
        public uint PeakWorkingSetSize;
        public uint WorkingSetSize;
        public uint QuotaPeakPagedPoolUsage;
        public uint QuotaPagedPoolUsage;
        public uint QuotaPeakNonPagedPoolUsage;
        public uint QuotaNonPagedPoolUsage;
        public uint PagefileUsage;
        public uint PeakPagefileUsage;
    }
    [StructLayout(LayoutKind.Sequential, Size = 40)]
    [DllImport("psapi.dll", SetLastError = true)]
    unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS* Memcounters, int size);

this is the calss that implements native pinvok codes

(at the beginning i was trying to test native vs managed / .net approach ) but before even trying the pinvoke vs .net i dived strait to check on performance btween pinvoke *unsafe vs .net

so this part is still in "safe mode" should it be also treated with the astrics ?

static class Nat
    {
        public static class IO
        {
            public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
                GetProcessIoCounters(System.Diagnostics.Process.GetCurrentProcess().Handle, out counters);
                retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
                retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
                retCountIoDict.Add("OtherOperationCount", counters.OtherOperationCount);
                retCountIoDict.Add("ReadTransferCount", counters.ReadTransferCount);
                retCountIoDict.Add("WriteTransferCount", counters.WriteTransferCount);
                retCountIoDict.Add("OtherTransferCount", counters.OtherTransferCount);
                return retCountIoDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";

            }
        }
        public static class Mem
        {
            public static Dictionary<string, uint> GetAllMem(Process procToRtrivMem)
            {

                PROCESS_MEMORY_COUNTERS MemCounters;
                Dictionary<string, uint> retCountMemDict = new Dictionary<string, uint>();
                GetProcessMemoryInfo(System.Diagnostics.Process.GetCurrentProcess().Handle, out MemCounters, Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS))); //MemCounters.cb);
                retCountMemDict.Add("cb", MemCounters.cb);
                retCountMemDict.Add("PageFaultCount", MemCounters.PageFaultCount);
                retCountMemDict.Add("PeakWorkingSetSize", MemCounters.PeakWorkingSetSize);
                retCountMemDict.Add("WorkingSetSize", MemCounters.WorkingSetSize);
                retCountMemDict.Add("QuotaPeakPagedPoolUsage", MemCounters.QuotaPeakPagedPoolUsage);
                retCountMemDict.Add("QuotaPagedPoolUsage", MemCounters.QuotaPagedPoolUsage);

                retCountMemDict.Add("QuotaPeakNonPagedPoolUsage", MemCounters.QuotaPeakNonPagedPoolUsage);
                retCountMemDict.Add("QuotaNonPagedPoolUsage", MemCounters.QuotaNonPagedPoolUsage);
                retCountMemDict.Add("PagefileUsage", MemCounters.PagefileUsage);
                retCountMemDict.Add("PeakPagefileUsage", MemCounters.PeakPagefileUsage);

                return retCountMemDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";

            }
        }

    }

sorry for that, but the update is i missed the part of : compiler option

compilerOptions="/unsafe" // <<-- you can add this to the compiler config line.

though still . my problems only started then cause there is not only one error :

Unsafe code may only appear if compiling with /unsafe 

but errors all over the place !

first one for instance :

            public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();

            --->>   GetProcessIoCounters(System.Diagnostics.Process.GetCurrentProcess().Handle, out counters);


                retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
                retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);

the error is for that line calling GetProcesIoCounters()

Error 3 Argument 1: cannot convert from 'System.IntPtr' to 'System.IntPtr*' g:\RobDevI5-Raid-0\Documents\Visual Studio 2010\WebSites\WebSite2\App_Code\CsExtensions.cs 416 42 g:...\WebSite2\

UPDAT - Code That seem to work though i could not verify it is using unsafe properly

signeture with unsafe

    [DllImport("psapi.dll", SetLastError = true)]
    unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS Memcounters, int size);

code with my "unsafe"

        public static class IO
        {
            public static unsafe Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
                IntPtr* Hw = (IntPtr*)System.Diagnostics.Process.GetCurrentProcess().Handle;
                GetProcessIoCounters(Hw, out counters);
                retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
                retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
                retCountIoDict.Add("OtherOperationCount", counters.OtherOperationCount);
                retCountIoDict.Add("ReadTransferCount", counters.ReadTransferCount);
                retCountIoDict.Add("WriteTransferCount", counters.WriteTransferCount);
                retCountIoDict.Add("OtherTransferCount", counters.OtherTransferCount);
                return retCountIoDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";

            }
        }

Solution

  • Ok you need to change a few things, shown below

    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System;
    using System.Diagnostics;
    static class Nat
    {
        [StructLayout(LayoutKind.Sequential]
        struct IO_COUNTERS
        {
            public ulong ReadOperationCount;
            public ulong WriteOperationCount;
            public ulong OtherOperationCount;
            public ulong ReadTransferCount;
            public ulong WriteTransferCount;
            public ulong OtherTransferCount;
        }
        [DllImport("kernel32.dll")]
        unsafe static extern bool GetProcessIoCounters(IntPtr ProcessHandle, out IO_COUNTERS IoCounters);
    
        [StructLayout(LayoutKind.Sequential, Size = 40)]
        private struct PROCESS_MEMORY_COUNTERS
        {
            public uint cb;
            public uint PageFaultCount;
            public uint PeakWorkingSetSize;
            public uint WorkingSetSize;
            public uint QuotaPeakPagedPoolUsage;
            public uint QuotaPagedPoolUsage;
            public uint QuotaPeakNonPagedPoolUsage;
            public uint QuotaNonPagedPoolUsage;
            public uint PagefileUsage;
            public uint PeakPagefileUsage;
        }
    
        [DllImport("psapi.dll", SetLastError = true)]
        unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS* Memcounters, int size);
    
        public static class IO
        {
            unsafe public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
                IntPtr ptr = System.Diagnostics.Process.GetCurrentProcess().Handle;
    
                GetProcessIoCounters(ptr, out counters);
                retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
                retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
                retCountIoDict.Add("OtherOperationCount", counters.OtherOperationCount);
                retCountIoDict.Add("ReadTransferCount", counters.ReadTransferCount);
                retCountIoDict.Add("WriteTransferCount", counters.WriteTransferCount);
                retCountIoDict.Add("OtherTransferCount", counters.OtherTransferCount);
                return retCountIoDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";
    
            }
        }
        public static class Mem
        {
            unsafe public static Dictionary<string, uint> GetAllMem(Process procToRtrivMem)
            {
    
                PROCESS_MEMORY_COUNTERS* MemCounters;
                Dictionary<string, uint> retCountMemDict = new Dictionary<string, uint>();
                IntPtr ptr = System.Diagnostics.Process.GetCurrentProcess().Handle;
    
                GetProcessMemoryInfo(&ptr, out MemCounters, Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS))); //MemCounters.cb);
                retCountMemDict.Add("cb", MemCounters->cb);
                retCountMemDict.Add("PageFaultCount", MemCounters->PageFaultCount);
                retCountMemDict.Add("PeakWorkingSetSize", MemCounters->PeakWorkingSetSize);
                retCountMemDict.Add("WorkingSetSize", MemCounters->WorkingSetSize);
                retCountMemDict.Add("QuotaPeakPagedPoolUsage", MemCounters->QuotaPeakPagedPoolUsage);
                retCountMemDict.Add("QuotaPagedPoolUsage", MemCounters->QuotaPagedPoolUsage);
    
                retCountMemDict.Add("QuotaPeakNonPagedPoolUsage", MemCounters->QuotaPeakNonPagedPoolUsage);
                retCountMemDict.Add("QuotaNonPagedPoolUsage", MemCounters->QuotaNonPagedPoolUsage);
                retCountMemDict.Add("PagefileUsage", MemCounters->PagefileUsage);
                retCountMemDict.Add("PeakPagefileUsage", MemCounters->PeakPagefileUsage);
    
                return retCountMemDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";
    
            }
        }
    
    }