Skip to content

Commit a1e9421

Browse files
cpuguy83runcom
authored andcommitted
Use sync.Pool for logger Messages
This reduces allocs and bytes used per log entry significantly as well as some improvement to time per log operation. Each log driver, however, must put messages back in the pool once they are finished with the message. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 252c2f3 commit a1e9421

14 files changed

Lines changed: 74 additions & 25 deletions

File tree

daemon/logger/awslogs/cloudwatchlogs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ func (l *logStream) Log(msg *logger.Message) error {
175175
l.lock.RLock()
176176
defer l.lock.RUnlock()
177177
if !l.closed {
178-
// buffer up the data, making sure to copy the Line data
179178
l.messages <- msg
180179
}
181180
return nil
@@ -277,6 +276,7 @@ func (l *logStream) collectBatch() {
277276
})
278277
bytes += (lineBytes + perEventBytes)
279278
}
279+
logger.PutMessage(msg)
280280
}
281281
}
282282
}

daemon/logger/copier.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,14 @@ func (c *Copier) copySrc(name string, src io.Reader) {
7676
}
7777
// Break up the data that we've buffered up into lines, and log each in turn.
7878
p := 0
79-
for q := bytes.Index(buf[p:n], []byte{'\n'}); q >= 0; q = bytes.Index(buf[p:n], []byte{'\n'}) {
79+
for q := bytes.IndexByte(buf[p:n], '\n'); q >= 0; q = bytes.IndexByte(buf[p:n], '\n') {
8080
select {
8181
case <-c.closed:
8282
return
8383
default:
84-
msg := &Message{
85-
Source: name,
86-
Timestamp: time.Now().UTC(),
87-
}
84+
msg := NewMessage()
85+
msg.Source = name
86+
msg.Timestamp = time.Now().UTC()
8887
msg.Line = append(msg.Line, buf[p:p+q]...)
8988

9089
if logErr := c.dst.Log(msg); logErr != nil {
@@ -98,11 +97,9 @@ func (c *Copier) copySrc(name string, src io.Reader) {
9897
// noting that it's a partial log line.
9998
if eof || (p == 0 && n == len(buf)) {
10099
if p < n {
101-
msg := &Message{
102-
Source: name,
103-
Timestamp: time.Now().UTC(),
104-
Partial: true,
105-
}
100+
msg := NewMessage()
101+
msg.Source = name
102+
msg.Timestamp = time.Now().UTC()
106103
msg.Line = append(msg.Line, buf[p:n]...)
107104
msg.Partial = true
108105

daemon/logger/copier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestCopierSlow(t *testing.T) {
208208
type BenchmarkLoggerDummy struct {
209209
}
210210

211-
func (l *BenchmarkLoggerDummy) Log(m *Message) error { return nil }
211+
func (l *BenchmarkLoggerDummy) Log(m *Message) error { PutMessage(m); return nil }
212212

213213
func (l *BenchmarkLoggerDummy) Close() error { return nil }
214214

daemon/logger/etwlogs/etwlogs_windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func (etwLogger *etwLogs) Log(msg *logger.Message) error {
7676
logrus.Error(errorMessage)
7777
return errors.New(errorMessage)
7878
}
79-
return callEventWriteString(createLogMessage(etwLogger, msg))
79+
m := createLogMessage(etwLogger, msg)
80+
logger.PutMessage(msg)
81+
return callEventWriteString(m)
8082
}
8183

8284
// Close closes the logger by unregistering the ETW provider.

daemon/logger/fluentd/fluentd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ func (f *fluentd) Log(msg *logger.Message) error {
151151
for k, v := range f.extra {
152152
data[k] = v
153153
}
154+
155+
ts := msg.Timestamp
156+
logger.PutMessage(msg)
154157
// fluent-logger-golang buffers logs from failures and disconnections,
155158
// and these are transferred again automatically.
156-
return f.writer.PostWithTime(f.tag, msg.Timestamp, data)
159+
return f.writer.PostWithTime(f.tag, ts, data)
157160
}
158161

159162
func (f *fluentd) Close() error {

daemon/logger/gcplogs/gcplogging.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,16 @@ func ValidateLogOpts(cfg map[string]string) error {
185185
}
186186

187187
func (l *gcplogs) Log(m *logger.Message) error {
188+
data := string(m.Line)
189+
ts := m.Timestamp
190+
logger.PutMessage(m)
191+
188192
l.logger.Log(logging.Entry{
189-
Timestamp: m.Timestamp,
193+
Timestamp: ts,
190194
Payload: &dockerLogEntry{
191195
Instance: l.instance,
192196
Container: l.container,
193-
Data: string(m.Line),
197+
Data: data,
194198
},
195199
})
196200
return nil

daemon/logger/gelf/gelf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (s *gelfLogger) Log(msg *logger.Message) error {
133133
Level: level,
134134
RawExtra: s.rawExtra,
135135
}
136+
logger.PutMessage(msg)
136137

137138
if err := s.writer.WriteMessage(&m); err != nil {
138139
return fmt.Errorf("gelf: cannot send GELF message: %v", err)

daemon/logger/journald/journald.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,14 @@ func (s *journald) Log(msg *logger.Message) error {
105105
if msg.Partial {
106106
vars["CONTAINER_PARTIAL_MESSAGE"] = "true"
107107
}
108+
109+
line := string(msg.Line)
110+
logger.PutMessage(msg)
111+
108112
if msg.Source == "stderr" {
109-
return journal.Send(string(msg.Line), journal.PriErr, vars)
113+
return journal.Send(line, journal.PriErr, vars)
110114
}
111-
return journal.Send(string(msg.Line), journal.PriInfo, vars)
115+
return journal.Send(line, journal.PriInfo, vars)
112116
}
113117

114118
func (s *journald) Name() string {

daemon/logger/jsonfilelog/jsonfilelog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
100100
Created: timestamp,
101101
RawAttrs: l.extra,
102102
}).MarshalJSONBuf(l.buf)
103+
logger.PutMessage(msg)
103104
if err != nil {
104105
l.mu.Unlock()
105106
return err

daemon/logger/logentries/logentries.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func (f *logentries) Log(msg *logger.Message) error {
6161
for k, v := range f.extra {
6262
data[k] = v
6363
}
64-
f.writer.Println(f.tag, msg.Timestamp, data)
64+
ts := msg.Timestamp
65+
logger.PutMessage(msg)
66+
f.writer.Println(f.tag, ts, data)
6567
return nil
6668
}
6769

0 commit comments

Comments
 (0)