@@ -138,19 +138,22 @@ private[spark] class ProcfsMetricsGetter(
138138 }
139139 val stdoutThread = Utils .processStreamByLine(" read stdout for pgrep" ,
140140 process.getInputStream, appendChildPid)
141- val error = process.getErrorStream
142- var errorString = " "
143- (0 until error.available()).foreach { i =>
144- errorString += error.read()
145- }
141+ val errorStringBuilder = new StringBuilder ()
142+ val stdErrThread = Utils .processStreamByLine(
143+ " stderr for pgrep" ,
144+ process.getErrorStream,
145+ { line =>
146+ errorStringBuilder.append(line)
147+ })
146148 val exitCode = process.waitFor()
147149 stdoutThread.join()
150+ stdErrThread.join()
151+ val errorString = errorStringBuilder.toString()
148152 // pgrep will have exit code of 1 if there are more than one child process
149153 // and it will have a exit code of 2 if there is no child process
150154 if (exitCode != 0 && exitCode > 2 ) {
151155 val cmd = builder.command().toArray.mkString(" " )
152- logWarning(s " Process $cmd" +
153- s " exited with code $exitCode, with stderr: " + s " ${errorString} " )
156+ logWarning(s " Process $cmd exited with code $exitCode and stderr: $errorString" )
154157 throw new SparkException (s " Process $cmd exited with code $exitCode" )
155158 }
156159 childPidsInInt
@@ -165,12 +168,9 @@ private[spark] class ProcfsMetricsGetter(
165168
166169 def addProcfsMetricsFromOneProcess (
167170 allMetrics : ProcfsMetrics ,
168- pid : Int ):
169- ProcfsMetrics = {
171+ pid : Int ): ProcfsMetrics = {
170172
171- // Hadoop ProcfsBasedProcessTree class used regex and pattern matching to retrive the memory
172- // info. I tried that but found it not correct during tests, so I used normal string analysis
173- // instead. The computation of RSS and Vmem are based on proc(5):
173+ // The computation of RSS and Vmem are based on proc(5):
174174 // http://man7.org/linux/man-pages/man5/proc.5.html
175175 try {
176176 val pidDir = new File (procfsDir, pid.toString)
@@ -180,28 +180,25 @@ private[spark] class ProcfsMetricsGetter(
180180 Utils .tryWithResource( new BufferedReader (fReader)) { in =>
181181 val procInfo = in.readLine
182182 val procInfoSplit = procInfo.split(" " )
183- if (procInfoSplit != null ) {
184- val vmem = procInfoSplit(22 ).toLong
185- val rssPages = procInfoSplit(23 ).toLong
186- if (procInfoSplit(1 ).toLowerCase(Locale .US ).contains(" java" )) {
187- return allMetrics.copy(
188- jvmVmemTotal = allMetrics.jvmVmemTotal + vmem,
189- jvmRSSTotal = allMetrics.jvmRSSTotal + (rssPages* pageSize)
190- )
191- }
192- else if (procInfoSplit(1 ).toLowerCase(Locale .US ).contains(" python" )) {
193- return allMetrics.copy(
194- pythonVmemTotal = allMetrics.pythonVmemTotal + vmem,
195- pythonRSSTotal = allMetrics.pythonRSSTotal + (rssPages* pageSize)
196- )
197- }
198- return allMetrics.copy(
199- otherVmemTotal = allMetrics.otherVmemTotal + vmem,
200- otherRSSTotal = allMetrics.otherRSSTotal + (rssPages* pageSize)
201- )
183+ val vmem = procInfoSplit(22 ).toLong
184+ val rssMem = procInfoSplit(23 ).toLong* pageSize
185+ if (procInfoSplit(1 ).toLowerCase(Locale .US ).contains(" java" )) {
186+ allMetrics.copy(
187+ jvmVmemTotal = allMetrics.jvmVmemTotal + vmem,
188+ jvmRSSTotal = allMetrics.jvmRSSTotal + (rssMem)
189+ )
190+ }
191+ else if (procInfoSplit(1 ).toLowerCase(Locale .US ).contains(" python" )) {
192+ allMetrics.copy(
193+ pythonVmemTotal = allMetrics.pythonVmemTotal + vmem,
194+ pythonRSSTotal = allMetrics.pythonRSSTotal + (rssMem)
195+ )
202196 }
203197 else {
204- return ProcfsMetrics (0 , 0 , 0 , 0 , 0 , 0 )
198+ allMetrics.copy(
199+ otherVmemTotal = allMetrics.otherVmemTotal + vmem,
200+ otherRSSTotal = allMetrics.otherRSSTotal + (rssMem)
201+ )
205202 }
206203 }
207204 }
0 commit comments