From adb1044f1a5cb8f25178f5f5e6106d8adb3d4109 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期一, 13 一月 2020 10:48:03 +0800 Subject: [PATCH] debug --- run.go | 221 +++++++++++++++++++++++++++++++------------------------ 1 files changed, 125 insertions(+), 96 deletions(-) diff --git a/run.go b/run.go index ede5e89..adda50a 100644 --- a/run.go +++ b/run.go @@ -12,15 +12,92 @@ "basic.com/valib/gogpu.git" ) +type channel struct { + index int + life int +} + +func (f *face) channelAvailable() int { + f.mapChanLock.Lock() + defer f.mapChanLock.Unlock() + + for i := 0; i < f.maxChannel; i++ { + found := false + for _, v := range f.mapChan { + if v.index == i { + found = true + break + } + } + if !found { + return i + } + } + return -1 +} + +func (f *face) channelExist(id string) (*channel, bool) { + f.mapChanLock.Lock() + defer f.mapChanLock.Unlock() + + c, ok := f.mapChan[id] + return c, ok +} + +func (f *face) channelNew(id string) *channel { + + index := f.channelAvailable() + if index < 0 { + return nil + } + + f.mapChanLock.Lock() + c := &channel{ + index: index, + life: f.maxLife, + } + f.mapChan[id] = c + f.mapChanLock.Unlock() + + return c +} + +func (f *face) maintainChannel(ctx context.Context, ch <-chan string) { + for { + select { + case <-ctx.Done(): + return + case id := <-ch: + f.mapChanLock.Lock() + for k, v := range f.mapChan { + v.life-- + if k == id { + v.life = f.maxLife + } + } + for k, v := range f.mapChan { + if v.life < 0 { + delete(f.mapChan, k) + } + } + f.mapChanLock.Unlock() + + default: + time.Sleep(10 * time.Millisecond) + } + } +} + type face struct { handle *SDKFace list *sdkhelper.LockList - maxChannel int - ftrackChans map[string]chan protomsg.SdkMessage - ftrackChannels map[string]int - ftrackChanStats []bool - chnLock sync.Mutex + maxChannel int + + maxLife int + chLife chan string + mapChan map[string]*channel + mapChanLock sync.Mutex fnLogger func(...interface{}) @@ -103,10 +180,10 @@ handle: handle, list: sdkhelper.NewLockList(maxChan + maxChan/2), - maxChannel: maxChan, - ftrackChans: make(map[string]chan protomsg.SdkMessage, maxChan), - ftrackChannels: make(map[string]int, maxChan), - ftrackChanStats: make([]bool, maxChan, maxChan), + maxLife: trckInterval * 10, + chLife: make(chan string, maxChan), + maxChannel: maxChan, + mapChan: make(map[string]*channel, maxChan), fnLogger: fn, @@ -129,25 +206,30 @@ func Run(ctx context.Context, i interface{}) { s := i.(*face) + go s.maintainChannel(ctx, s.chLife) + chRcv, chSnd := sdkhelper.FlowCreate(ctx, s.id, s.shm, s.ipc2Rule, s.ruleMsgMaxCacheSize, s.fnLogger) go sdkhelper.FlowBatch(ctx, chRcv, chSnd, s.typ, s.list.Push, s.list.Drain, s.run, s.release, s.fnLogger) } func (f *face) run(msgs []protomsg.SdkMessage, out chan<- sdkstruct.MsgSDK, typ string) { + wg := &sync.WaitGroup{} + for _, msg := range msgs { - if _, ok := f.ftrackChans[msg.Cid]; ok { - f.ftrackChans[msg.Cid] <- msg + f.chLife <- msg.Cid + + if c, ok := f.channelExist(msg.Cid); ok { + wg.Add(1) + go f.track(wg, &msg, c.index, out) } else { - f.ftrackChans[msg.Cid] = make(chan protomsg.SdkMessage, cacheFrameNum) - chn := f.getAvailableChn() - if chn < 0 { + nc := f.channelNew(msg.Cid) + if nc == nil { f.fnLogger("TOO MUCH CHANNEL") sdkhelper.EjectResult(nil, msg, out) continue } - f.ftrackChannels[msg.Cid] = chn i := sdkhelper.UnpackImage(msg, f.typ, f.fnLogger) if i == nil { @@ -156,101 +238,48 @@ } // conv to bgr24 and resize imgW, imgH := int(i.Width), int(i.Height) - ret := f.handle.TrackerResize(imgW, imgH, chn) - f.fnLogger("ResizeFaceTracker: cid: ", msg.Cid, " chan: ", chn, " wXh: ", imgW, "x", imgH, " result:", ret) - go f.detectTrackOneChn(f.ftrackChans[msg.Cid], out, chn) - f.ftrackChans[msg.Cid] <- msg + ret := f.handle.TrackerResize(imgW, imgH, nc.index) + f.fnLogger("ResizeFaceTracker: cid: ", msg.Cid, " chan: ", nc.index, " wXh: ", imgW, "x", imgH, " result:", ret) + wg.Add(1) + go f.track(wg, &msg, nc.index, out) } } + wg.Wait() + f.fnLogger("######Wait for All track") } -func (f *face) detectTrackOneChn(in <-chan protomsg.SdkMessage, out chan<- sdkstruct.MsgSDK, dtchn int) { - tm := time.Now() - sc := 0 - f.fnLogger("DETECTTRACKONECHN DTCHN: ", dtchn) - var curCid string +func (f *face) track(wg *sync.WaitGroup, msg *protomsg.SdkMessage, tchan int, out chan<- sdkstruct.MsgSDK) { - for { - select { - case rMsg := <-in: + defer wg.Done() - if !sdkhelper.ValidRemoteMessage(rMsg, f.typ, f.fnLogger) { - sdkhelper.EjectResult(nil, rMsg, out) - continue - } + rMsg := *msg - i := sdkhelper.UnpackImage(rMsg, f.typ, f.fnLogger) - if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 { - sdkhelper.EjectResult(nil, rMsg, out) - continue - } + if !sdkhelper.ValidRemoteMessage(rMsg, f.typ, f.fnLogger) { + sdkhelper.EjectResult(nil, rMsg, out) + f.fnLogger("Face!!!!!!SkdMessage Invalid: ", tchan) - curCid = i.Cid - - // conv to bgr24 and resize - imgW, imgH := int(i.Width), int(i.Height) - - count, data, _ := f.handle.Run(i.Data, imgW, imgH, 3, dtchn) - - sdkhelper.EjectResult(data, rMsg, out) - var id, name string - if rMsg.Tasklab != nil { - id, name = rMsg.Tasklab.Taskid, rMsg.Tasklab.Taskname - } - f.fnLogger("CAMERAID: ", rMsg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT ", f.typ, " COUNT: ", count) - - sc++ - if sc == 25 { - f.fnLogger("CHAN:%d, FACE RUN 25 FRAME USE TIME: ", dtchn, time.Since(tm)) - sc = 0 - tm = time.Now() - } - - if time.Since(tm) > time.Second { - f.fnLogger("CHAN: ", dtchn, " FACE RUN ", sc, " FRAME USE TIME: ", time.Since(tm)) - sc = 0 - tm = time.Now() - } - case <-time.After(trackChnTimeout * time.Second): - f.fnLogger("Timeout to get image, curCid:", curCid) - if curCid != "" { - delete(f.ftrackChans, curCid) - f.releaseChn(dtchn) - } - return - } + return } -} -////////////////////////////////////////////////////////////////// -const ( - cacheFrameNum = 3 - trackChnTimeout = time.Duration(10) -) + i := sdkhelper.UnpackImage(rMsg, f.typ, f.fnLogger) + if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 { + sdkhelper.EjectResult(nil, rMsg, out) + f.fnLogger("Face!!!!!!Unpack Image From SkdMessage Failed: ", tchan) -func (f *face) cleanChnStat() { - f.chnLock.Lock() - for i := 0; i < f.maxChannel; i++ { - f.ftrackChanStats[i] = false + return } - f.chnLock.Unlock() -} -func (f *face) getAvailableChn() int { - f.chnLock.Lock() - defer f.chnLock.Unlock() + // conv to bgr24 and resize + imgW, imgH := int(i.Width), int(i.Height) - for i := 0; i < f.maxChannel; i++ { - if f.ftrackChanStats[i] == false { - f.ftrackChanStats[i] = true - return i - } + count, data, _ := f.handle.Run(i.Data, imgW, imgH, 3, tchan) + + sdkhelper.EjectResult(data, rMsg, out) + + var id, name string + if rMsg.Tasklab != nil { + id, name = rMsg.Tasklab.Taskid, rMsg.Tasklab.Taskname } - return -1 -} + f.fnLogger("Chan: ", tchan, "CAMERAID: ", rMsg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT ", f.typ, " COUNT: ", count) -func (f *face) releaseChn(chn int) { - f.chnLock.Lock() - f.ftrackChanStats[chn] = false - f.chnLock.Unlock() } -- Gitblit v1.8.0