Home Forums Programming CLR Integration and Programming. CLR mountpoint freespace query scaling *really bad* with high number of mountpoints RE: CLR mountpoint freespace query scaling *really bad* with high number of mountpoints

  • Hi Solomon,

    Thanks for the all tips, and i will be sure to use them.

    By "But only to find out that .NET SQL CLR is "some what limited" in the things you can use." i ment that i got errors when i tried to use this construct in the CLR

    ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");

    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Volume where Drivetype = 3");

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

    ManagementObjectCollection queryCollection = searcher.Get();

    It seems that ManagementObjectSearcher is .NET 2 based an not allowed in a CLR.

    It would have solved my performance problem, because if i use it in my console app, it rips through all 139 mountpoints in 500 ms flat; hundreds of times faster then using the perfmon counter workaround.

    console app source

    -------------------------------------------------------------------------------------

    //-------------------------------

    // Win32_Volume version

    //-------------------------------

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Management;

    using System.IO;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main()

    {

    DateTime StartDT = DateTime.Now;

    DateTime StartStepDT;

    DateTime EndStepDT;

    double StepRuntimeMs;

    double EnumRuntimeMs;

    StartStepDT = DateTime.Now;

    ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");

    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Volume where Drivetype = 3");

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

    ManagementObjectCollection queryCollection = searcher.Get();

    EndStepDT = DateTime.Now;

    StepRuntimeMs = (EndStepDT - StartStepDT).TotalMilliseconds;

    Console.WriteLine("Init Runtime: {0, 8} ms", StepRuntimeMs);

    string sVolumeLabel = "";

    //string[,] saReturn = new string[queryCollection.Count, 7];

    int i = 0; // counter for foreach

    StartStepDT = DateTime.Now;

    foreach (ManagementObject m in queryCollection)

    {

    EndStepDT = DateTime.Now;

    EnumRuntimeMs = (EndStepDT - StartStepDT).TotalMilliseconds;

    StartStepDT = DateTime.Now;

    //if (string.IsNullOrEmpty(Convert.ToString(m["VolumeName"]))) { sVolumeLabel = "Local Disk"; } else { sVolumeLabel = Convert.ToString(m["VolumeName"]); } // Disk Label

    //string sSystemName = Convert.ToString(m["SystemName"]); // Name of computer

    string sDriveLetter = Convert.ToString(m["Name"]); // Drive Letter

    if (m["DeviceID"] != null)

    {

    decimal dSize = Math.Round((Convert.ToDecimal(m["Capacity"]) / 1048576), 2); //HDD Size in MB

    decimal dFree = Math.Round((Convert.ToDecimal(m["FreeSpace"]) / 1048576), 2); // Free Space in MB

    decimal dUsed = dSize - dFree; // Used HDD Space in MB

    //int iPercent = Convert.ToInt32((dFree / dSize) * 100); // Percentage of free space

    //saReturn[i, 0] = sSystemName;

    //saReturn[i, 1] = sDriveLetter;

    //saReturn[i, 2] = sVolumeLabel;

    //saReturn[i, 3] = Convert.ToString(dSize);

    //saReturn[i, 4] = Convert.ToString(dUsed);

    //saReturn[i, 5] = Convert.ToString(dFree);

    //saReturn[i, 6] = Convert.ToString(iPercent);

    Console.WriteLine("-----------------------------");

    //Console.WriteLine("SystemName : " + sSystemName);

    Console.WriteLine("DriveLetter : " + sDriveLetter);

    Console.WriteLine("VolumeLabel : " + sVolumeLabel);

    Console.WriteLine("Size : " + Convert.ToString(dSize));

    Console.WriteLine("Used : " + Convert.ToString(dUsed));

    Console.WriteLine("Free : " + Convert.ToString(dFree));

    i++; // increase counter. This will add the above details for the next drive.

    }

    else

    {

    Console.WriteLine("-----------------------------");

    //Console.WriteLine("SystemName : " + sSystemName);

    Console.WriteLine("DriveLetter : " + sDriveLetter);

    }

    EndStepDT = DateTime.Now;

    StepRuntimeMs = (EndStepDT - StartStepDT).TotalMilliseconds;

    Console.WriteLine(" Enum Runtime: {0, 8} ms", EnumRuntimeMs);

    Console.WriteLine(" Step Runtime: {0, 8} ms", StepRuntimeMs);

    StartStepDT = DateTime.Now;

    }

    Console.WriteLine("");

    DateTime EndDT = DateTime.Now;

    double RuntimeMs = (EndDT - StartDT).TotalMilliseconds;

    Console.WriteLine("");

    Console.WriteLine("Runtime: {0, 8} ms", RuntimeMs);

    }

    }

    }

    -----------------------------------------------------------------------------------------------------

    Since this runs really reliable in an command shell i'm tempted to get the data using xp_cmdshell and a 6 KB console app.

    Unless of course you know a way to get to get 2 things workin; mountpoint enumeration and getting size, free and volumename?

    Grtz, t.