@@ -86,8 +86,7 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
8686 if f == nil {
8787 return nil
8888 }
89- err = sem .Acquire (ctx , 2 )
90- if err != nil {
89+ if err := sem .Acquire (ctx , 2 ); err != nil {
9190 return err
9291 }
9392 defer sem .Release (2 )
@@ -109,46 +108,49 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
109108 if cPath == "" {
110109 return nil
111110 }
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 {
133- _ = f .Close ()
134- return err
135- }
136- pid , err := strconv .Atoi (string (line ))
111+ {
112+ f , err := os .Open (filepath .Clean (filepath .Join (path , "cgroup.procs" )))
137113 if err != nil {
138114 _ = f .Close ()
139- return err
115+ return nil
140116 }
141- err = sem .Acquire (ctx , gproc .RequiredWeight )
142- if err != nil {
143- _ = f .Close ()
144- return err
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 .Collect (cPath )
127+ gproc .Exists = true
128+ reader := bufio .NewReaderSize (f , 1028 )
129+ for {
130+ line , _ , err := reader .ReadLine ()
131+ if err == io .EOF {
132+ break
133+ } else if err != nil {
134+ _ = f .Close ()
135+ return err
136+ }
137+ pid , err := strconv .Atoi (string (line ))
138+ if err != nil {
139+ _ = f .Close ()
140+ return err
141+ }
142+ err = sem .Acquire (ctx , gproc .RequiredWeight )
143+ if err != nil {
144+ _ = f .Close ()
145+ return err
146+ }
147+ wg .Add (1 )
148+ go func (wg * sync.WaitGroup , pid int , gproc * grouped_proc.GroupedProc ) {
149+ _ = gproc .AppendProcAndCollect (pid )
150+ sem .Release (gproc .RequiredWeight )
151+ wg .Done ()
152+ }(wg , pid , gproc )
145153 }
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 )
152154 }
153155 return nil
154156 })
0 commit comments