21

I'm trying to program in a performance counter into my C# application that launches another process and checks the processor usage of that launched process. As I understand it, the performance counter class requires me to assign a category name , a counter name, and a process name. I can get the process name easily enough, but is there a list of some sort on the internet that has all possible category and counter names I can assign? I tried scouring MSDN for something like this, but I didn't find anything.

Thanks for all the help!

Waffles
  • 289
  • 2
  • 3
  • 7

4 Answers4

25

I think you want to know what aspects of the process you can monitor. A list of the Process Performance Counters is available here Nevertheless you can use the GetCategories static method to list all categories in the machine or you could be more specific and create the PerformanceCategory for the "Process" category and use the GetCounters to get a list of all counters available. Hope this helps.

CriGoT
  • 954
  • 7
  • 12
  • 5
    This class is so confusing to use! Why didn't they use enums instead of strings consisting of so many complicated characters! – TheGateKeeper Apr 16 '12 at 21:28
  • 2
    My guess is that it is base on the fact that every product team (Windows, IIS, etc) "owns" the counters names therefore they can add/remove/change any name at any point on time. Besides this way we all can create our own set of counters. – CriGoT Apr 18 '12 at 13:04
  • I see no point in creating a counter to monitor custom data, you can just do it programmatically. – TheGateKeeper Apr 18 '12 at 16:17
  • The list of categories is so huge. Also, each category has its own list of counter names. It is so cool to have such a strong abstraction over such details of the machine. –  Dec 18 '19 at 04:25
5

For those who want to quickly browse and find needed Counter here's a quick Form that displays three list boxes with |Categories|Instances|Counters| and a Counter Value that is updated on a timer. With filters.

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;

namespace CountersListPreview
{
    internal static class CounterPreview
    {
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form f = new CountersListPreviewForm();
            Application.Run(f);
        }
    }

    internal class CountersListPreviewForm : Form
    {
        public CountersListPreviewForm()
        {
            InitializeComponent();
        }

        private PerformanceCounterCategory[] allCats;
        private string[] instances;
        private PerformanceCounter[] counters;
        private PerformanceCounter counter;
        private Timer TitleRefreshTimer;

        private void Form1_Load(object sender, EventArgs e)
        {
            allCats = PerformanceCounterCategory.GetCategories();
            listBox1.DataSource = allCats;
            listBox1.DisplayMember = "CategoryName";

            listBox1.SelectedIndexChanged += On_CatChange;
            listBox2.SelectedIndexChanged += On_InstChange;
            listBox3.SelectedIndexChanged += On_CounterChange;

            textBox2.TextChanged += On_CatFilterChanged;
            textBox3.TextChanged += On_InstFilterChanged;
            textBox4.TextChanged += On_CounterFilterChanged;

            TitleRefreshTimer = new Timer();
            TitleRefreshTimer.Tick += On_Timer;
            TitleRefreshTimer.Interval = 500;
            TitleRefreshTimer.Start();
        }

        private void On_Timer(object sender, EventArgs e)
        {
            textBox1.Text = counter != null ? counter.NextValue().ToString() : "";
        }

        // --------------- SELECTION CHANGE ------------------

        private void On_CatChange(object sender, EventArgs e)
        {
            var cat = listBox1.SelectedItem as PerformanceCounterCategory;
            listBox2.DataSource = instances = cat.GetInstanceNames();
        }

        private void On_InstChange(object sender, EventArgs e)
        {
            var cat = listBox1.SelectedItem as PerformanceCounterCategory;
            var inst = listBox2.SelectedItem as string;
            listBox3.DataSource = counters = cat.GetCounters(inst);
            listBox3.DisplayMember = "CounterName";
        }

        private void On_CounterChange(object sender, EventArgs e)
        {
            counter = listBox3.SelectedItem as PerformanceCounter;
            On_Timer(null, null);
        }

        // --------------- FILTERS ------------------

        private void On_CatFilterChanged(object sender, EventArgs e)
        {
            var filter = textBox2.Text;
            listBox1.DataSource = !string.IsNullOrEmpty(filter) 
                ? allCats.Where(cat => cat.CategoryName.ToLower().Contains(filter.ToLower())).ToArray() 
                : allCats;
        }

        private void On_InstFilterChanged(object sender, EventArgs e)
        {
            var filter = textBox3.Text;
            listBox2.DataSource = !string.IsNullOrEmpty(filter) 
                ? instances.Where(inst => inst.ToLower().Contains(filter.ToLower())).ToArray() 
                : instances;
        }

        private void On_CounterFilterChanged(object sender, EventArgs e)
        {
            var filter = textBox4.Text;
            listBox3.DataSource = !string.IsNullOrEmpty(filter) 
                ? counters.Where(c => c.CounterName.ToLower().Contains(filter.ToLower())).ToArray() 
                : counters;
        }

        // --------------- FORM AND LAYOUT ------------------

        private readonly IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null) components.Dispose();
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.listBox2 = new System.Windows.Forms.ListBox();
            this.listBox3 = new System.Windows.Forms.ListBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(12, 38);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(351, 524);
            this.listBox1.TabIndex = 3;
            // 
            // listBox2
            // 
            this.listBox2.FormattingEnabled = true;
            this.listBox2.Location = new System.Drawing.Point(369, 38);
            this.listBox2.Name = "listBox2";
            this.listBox2.Size = new System.Drawing.Size(351, 524);
            this.listBox2.TabIndex = 3;
            // 
            // listBox3
            // 
            this.listBox3.FormattingEnabled = true;
            this.listBox3.Location = new System.Drawing.Point(726, 38);
            this.listBox3.Name = "listBox3";
            this.listBox3.Size = new System.Drawing.Size(351, 524);
            this.listBox3.TabIndex = 3;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(726, 568);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(351, 20);
            this.textBox1.TabIndex = 4;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(606, 571);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(114, 13);
            this.label1.TabIndex = 5;
            this.label1.Text = "Counter Value (500ms)";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(12, 12);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(351, 20);
            this.textBox2.TabIndex = 4;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(369, 12);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(351, 20);
            this.textBox3.TabIndex = 4;
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(726, 12);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(351, 20);
            this.textBox4.TabIndex = 4;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            //this.BackColor = System.Drawing.SystemColors.;
            this.ClientSize = new System.Drawing.Size(1090, 597);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.listBox3);
            this.Controls.Add(this.listBox2);
            this.Controls.Add(this.listBox1);
            //this.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.Name = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion

        private ListBox listBox1;
        private ListBox listBox2;
        private ListBox listBox3;
        private TextBox textBox1;
        private Label label1;
        private TextBox textBox2;
        private TextBox textBox3;
        private TextBox textBox4;
    }
}
n1kk
  • 180
  • 2
  • 4
  • I just wanna say thanks for the helpful tool, but I've found a bit of an issue with this, it doesn't work properly on computers using a different language than english, specially for the categories. I tried to get the instances and counters for memory but since its called something else in my language, I just get an empty list for those. Could you maybe edit the code to account for this? – Simon Jensen Oct 13 '17 at 13:30
  • Hey @SimonJensen, thanks for the heads up. I'm getting a list of categories from the system, seems quite odd that it wont return the list, I don't have anything language specific in this example. But I'm bilingual also, so I can change my systems language and test it out. I'll try to find the issue when I'll have free time. – n1kk Oct 16 '17 at 07:34
4

I have created a method that show what CriGoT have writted above, a small shortcut.

    private static void GetAllCounters(string categoryFilter)
    {
        var categories = PerformanceCounterCategory.GetCategories();
        foreach (var cat in categories)
        {
            if (categoryFilter != null && categoryFilter.Length > 0)
            {
                if (!cat.CategoryName.Contains(categoryFilter)) continue;
            }
            Console.WriteLine("Category {0}", cat.CategoryName);
            try
            {
                var instances = cat.GetInstanceNames();
                if (instances != null && instances.Length > 0)
                {
                    foreach (var instance in instances)
                    {
                        //if (cat.CounterExists(instance))
                        //{
                            foreach (var counter in cat.GetCounters(instance))
                            {
                                Console.WriteLine("\tCounter Name {0} [{1}]", counter.CounterName, instance);
                            }
                        //}
                    }
                }
                else
                {
                    foreach (var counter in cat.GetCounters())
                    {
                        Console.WriteLine("\tCounter Name {0}", counter.CounterName);
                    }
                }
            }
            catch (Exception)
            {
                // NO COUNTERS
            }
        }
        Console.ReadLine();
}

:-)

antonio
  • 379
  • 8
  • 13
1

You can assign them whatever you want. The Performance Monitor will simply show whatever category you choose and whatever counter name you choose for your particular need.

CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
ccdc.Add(new CounterCreationData("Counter Title", "Counter Description", PerformanceCounterType.NumberOfItems32));
PerformanceCounterCategory.Create("My Counter Category", "Category Description", PerformanceCounterCategoryType.Unknown, ccdc);
Michael Bray
  • 14,338
  • 6
  • 39
  • 64