Example #1
1
func MustNewWinPdhCollector(name, prefix string, opts config.Config_win_pdh_collector) *win_pdh_collector {

	c := &win_pdh_collector{
		name:        name,
		enabled:     true,
		prefix:      prefix,
		interval:    opts.Interval.MustDuration(time.Second),
		config:      opts,
		hPdh:        pdh.NewPdhCollector(),
		map_queries: make(map[string]config.Config_win_pdh_query),
	}

	for _, q := range opts.Queries {
		if q.Metric == "" {
			logging.Errorf("Error Phd Collector metric is empty: %# v", pretty.Formatter(q))
			continue
		}

		c.hPdh.AddEnglishCounter(q.Query)
		if q.Tags == nil {
			q.Tags = newcore.AddTags.Copy()
		}

		if opts.Query_to_tag == true || q.Query_to_tag == true {
			q.Tags["query"] = q.Query
		}

		c.map_queries[q.Query] = q
	}
	logging.Tracef("MustNewWinPdhCollector:opts.Queries: %# v", pretty.Formatter(opts.Queries))
	logging.Tracef("MustNuewWinPdhCollector c.map_queries: %# v", pretty.Formatter(c.map_queries))
	return c
}
Example #2
0
// confirmed pdh_windows.go don't have memory leak on single instance
func TestPdh_mem_leak_single_instance(t *testing.T) {
	pc := pdh.NewPdhCollector()
	pc.AddEnglishCounter("\\Process(lib_pdh_test.test)\\Working Set - Private")
	data := pc.CollectData()
	defer pc.Close()

	tick := time.Tick(time.Second)
	done := time.After(time.Second * 100)

	first_value := 0
	last_value := 0
	delta := 0

	for {
		select {
		case <-tick:
			for _, d := range data {
				if first_value == 0 {
					first_value = int(d.Value) / 1024
				}
				last_value = int(d.Value) / 1024
				delta = last_value - first_value

				fmt.Printf("first: %d, last: %d, delta: %d\n", first_value, last_value, delta)
				if first_value != last_value {
					t.Errorf("delta happens:  delta: %d ", delta)
					return
				}
			}
		case <-done:
			return
		}
	}
}
Example #3
0
func TestPdh_mem_leak_multiple_instance(t *testing.T) {
	pc := pdh.NewPdhCollector()
	pc.AddEnglishCounter("\\Process(lib_pdh_test.test)\\Working Set - Private")
	// data := pc.CollectData()
	defer pc.Close()

	tick := time.Tick(time.Second)
	tickClose := time.Tick(time.Second * 2)
	done := time.After(time.Second * 100)

	first_value := 0
	last_value := 0
	delta := 0

	for {
		select {
		case <-tickClose:
			fmt.Println("close and recreate pdh collector")
			pc.Close()
			// pc = nil
			pc = pdh.NewPdhCollector()
			pc.AddEnglishCounter("\\Process(lib_pdh_test.test)\\Working Set - Private")
		case <-tick:
			for _, d := range pc.CollectData() {
				if first_value == 0 {
					first_value = int(d.Value) / 1024
				}
				last_value = int(d.Value) / 1024
				delta = last_value - first_value

				fmt.Printf("first: %d, last: %d, delta: %d\n", first_value, last_value, delta)
				// if first_value != last_value {
				//  t.Errorf("delta happens:  delta: %d ", delta)
				//  return
				// }
			}
		case <-done:
			return
		}
	}
}