1--[[ 2Copyright 2016 GitHub, Inc 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15]] 16local TracerPipe = class("TracerPipe") 17 18TracerPipe.static.TRACEFS = "/sys/kernel/debug/tracing" 19TracerPipe.static.fields = "%s+(.-)%-(%d+)%s+%[(%d+)%]%s+(....)%s+([%d%.]+):.-:%s+(.+)" 20 21function TracerPipe:close() 22 if self.pipe ~= nil then 23 self.pipe:close() 24 end 25end 26 27function TracerPipe:open() 28 if self.pipe == nil then 29 self.pipe = assert(io.open(TracerPipe.TRACEFS .. "/trace_pipe")) 30 end 31 return self.pipe 32end 33 34function TracerPipe:readline() 35 return self:open():read() 36end 37 38function TracerPipe:trace_fields() 39 while true do 40 local line = self:readline() 41 if not line and self.nonblocking then 42 return nil 43 end 44 45 if not line:starts("CPU:") then 46 local task, pid, cpu, flags, ts, msg = line:match(TracerPipe.fields) 47 if task ~= nil then 48 return task, tonumber(pid), tonumber(cpu), flags, tonumber(ts), msg 49 end 50 end 51 end 52end 53 54function TracerPipe:initialize(nonblocking) 55 self.nonblocking = nonblocking 56end 57 58return TracerPipe 59