@@ -38,8 +38,8 @@ var DefaultSubsystems = []string{
3838type Cgroup struct {
3939 fsPath string
4040 subsystems []string
41- nRe * regexp.Regexp
42- eRe * regexp.Regexp
41+ nRe * regexp.Regexp // normalize regexp
42+ eRe * regexp.Regexp // exclude regexp
4343}
4444
4545func (c * Cgroup ) Name () string {
@@ -106,48 +106,49 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
106106 cPath = matches [1 ]
107107 }
108108 }
109- if cPath != "" {
110- f , err := os .Open (filepath .Clean (filepath .Join (path , "cgroup.procs" )))
111- if err != nil {
109+ if cPath == "" {
110+ return nil
111+ }
112+ f , err := os .Open (filepath .Clean (filepath .Join (path , "cgroup.procs" )))
113+ if err != nil {
114+ _ = f .Close ()
115+ return nil
116+ }
117+ var (
118+ gproc * grouped_proc.GroupedProc
119+ ok bool
120+ )
121+ gproc , ok = gprocs .Load (cPath )
122+ if ! ok {
123+ gproc = grouped_proc .NewGroupedProc (enabled )
124+ gprocs .Store (cPath , gproc )
125+ }
126+ gproc .Exists = true
127+ reader := bufio .NewReaderSize (f , 1028 )
128+ for {
129+ line , _ , err := reader .ReadLine ()
130+ if err == io .EOF {
131+ break
132+ } else if err != nil {
112133 _ = f .Close ()
113- return nil
134+ return err
114135 }
115- var (
116- gproc * grouped_proc.GroupedProc
117- ok bool
118- )
119- gproc , ok = gprocs .Load (cPath )
120- if ! ok {
121- gproc = grouped_proc .NewGroupedProc (enabled )
122- gprocs .Store (cPath , gproc )
136+ pid , err := strconv .Atoi (string (line ))
137+ if err != nil {
138+ _ = f .Close ()
139+ return err
123140 }
124- gproc .Exists = true
125- reader := bufio .NewReaderSize (f , 1028 )
126- for {
127- line , _ , err := reader .ReadLine ()
128- if err == io .EOF {
129- break
130- } else if err != nil {
131- _ = f .Close ()
132- return err
133- }
134- pid , err := strconv .Atoi (string (line ))
135- if err != nil {
136- _ = f .Close ()
137- return err
138- }
139- err = sem .Acquire (ctx , gproc .RequiredWeight )
140- if err != nil {
141- _ = f .Close ()
142- return err
143- }
144- wg .Add (1 )
145- go func (wg * sync.WaitGroup , pid int , gproc * grouped_proc.GroupedProc ) {
146- _ = gproc .AppendProcAndCollect (pid )
147- sem .Release (gproc .RequiredWeight )
148- wg .Done ()
149- }(wg , pid , gproc )
141+ err = sem .Acquire (ctx , gproc .RequiredWeight )
142+ if err != nil {
143+ _ = f .Close ()
144+ return err
150145 }
146+ wg .Add (1 )
147+ go func (wg * sync.WaitGroup , pid int , gproc * grouped_proc.GroupedProc ) {
148+ _ = gproc .AppendProcAndCollect (pid )
149+ sem .Release (gproc .RequiredWeight )
150+ wg .Done ()
151+ }(wg , pid , gproc )
151152 }
152153 return nil
153154 })
0 commit comments