探讨php中error_log函数输出内容的原子性问题_PHP教程
探讨php中error_log函数输出内容的原子性问题
前几天跟同事讨论如何在一次login php调用中保证error_log输出的日志都有唯一的标识头,结论是玩家帐号+当前时间+随机数,在我们当前用户量级条件下应该是满足需求的,当然,这并不是本文讨论的重点。
在确定这个简单的方案之后,我在想两个问题,也是今天本文讨论的重点:
1.error_log调用是否可以保证输出内容是完整的?
2.如果是完整的,那么是怎样保证的?
求证开始了,起初以为error_log调用中会对目标文件加文件锁,直接看源码(php5.6.12):
from: basic_functions.c
PHPAPI int _php_error_log_ex(int opt_err, char *message, int message_len, char *opt, char *headers TSRMLS_DC) /* {{{ */ { php_stream *stream = NULL; switch (opt_err) { case 1: /*send an email */ if (!php_mail(opt, PHP error_log message, message, headers, NULL TSRMLS_CC)) { return FAILURE; } break; case 2: /*send to an address */ php_error_docref(NULL TSRMLS_CC, E_WARNING, TCP/IP option not available!); return FAILURE; break; case 3: /*save to a file */ stream = php_stream_open_wrapper(opt, a, IGNORE_URL_WIN | REPORT_ERRORS, NULL); if (!stream) { return FAILURE; } php_stream_write(stream, message, message_len); php_stream_close(stream); break; case 4: /* send to SAPI */ if (sapi_module.log_message) { sapi_module.log_message(message TSRMLS_CC); } else { return FAILURE; } break; default: php_log_err(message TSRMLS_CC); break; } return SUCCESS; } /* }}} */
from: streams.c
/* Writes a buffer directly to a stream, using multiple of the chunk size */ static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count TSRMLS_DC) { size_t didwrite = 0, towrite, justwrote; /* if we have a seekable stream we need to ensure that data is written at the * current stream->position. This means invalidating the read buffer and then * performing a low-level seek */ if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && stream->readpos != stream->writepos) { stream->readpos = stream->writepos = 0; stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position TSRMLS_CC); } while (count > 0) { towrite = count; if (towrite > stream->chunk_size) towrite = stream->chunk_size; justwrote = stream->ops->write(stream, buf, towrite TSRMLS_CC); /* convert justwrote to an integer, since normally it is unsigned */ if ((int)justwrote > 0) { buf += justwrote; count -= justwrote; didwrite += justwrote; /* Only screw with the buffer if we can seek, otherwise we lose data * buffered from fifos and sockets */ if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { stream->position += justwrote; } } else { break; } } return didwrite; }
通过看源码文件,error_log只是以O_APPEND模式打开文件,然后就是write buf,好吧,没有看到文件锁的影子。而问题也很明显,write可能会调用多次,那么也就基本认定msg并不保证被原子输出,多次的write使得这个问题更严重。
多次write不能保证原子操作,那么单次呢?
1.from:man write
If the file was open(2)ed with <strong>O_APPEND</strong>, the file offset is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic step.
2.from:man write
Atomic/non-atomic: A write is atomic if the whole amount written in one operation is not interleaved with data from any other process. This is useful when there are multiple writers sending data to a single reader. Applications need to know how large a write request can be expected to be performed atomically. This maximum is called {PIPE_BUF}. This volume of IEEE Std 1003.1-2001 does not say whether write requests for more than {PIPE_BUF} bytes are atomic, but requires that writes of {PIPE_BUF} or fewer bytes shall be atomic.
man说的大概意思就是如果是O_APPEND模式,文件尾的定位与write调用是原子操作,不会存在write时文件尾还需要再调整,导致错位。当前这个原子性,只保证open和之后的第一次write,后续的write就不保证了。像error_log那样如果buf过长导致了多次write肯定就不保证buf一次完整输出。
继续深究,那么单一次write是原子的吗?man也给出了答案,不是的,只有要写出的数据小于等于PIPE_BUF时才保证原子操作。
问题得到了理论上的解答,下面开始实验验证:
test_error_log.php
<!--?php $str = ; for($i = 0; $i < (int)$argv[2]; $i++) { $str = $str.$argv[1]; } for($i = 0; $i < (int)$argv[3]; $i++) { error_log($str. , 3, ./append.txt); } ?-->
filename = ./append.txt for line in open(filename): print len(line)
#!/bin/bash rm -f append.txt for ((counter=0; counter < 10; ++counter)) do php test_error_log.php $counter $1 $2 & done sleep 2 #python check_line.py > a.txt python check_line.py | sort | uniq -c

验证思路:在输出msg最后加换行符,如果输出的每行与初始buf大小相同,说明本次error_log完整输出,经验证,内核3.5.0-23上PIPE_BUF是8K,8K以内的error_log输出都可以保证完整,否则会存在错乱的风险

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Go語言提供了兩種動態函數創建技術:closures和反射。 closures允許存取閉包作用域內的變量,而反射可使用FuncOf函數建立新函數。這些技術在自訂HTTP路由器、實現高度可自訂的系統和建置可插拔的元件方面非常有用。

在C++函數命名中,考慮參數順序至關重要,可提高可讀性、減少錯誤並促進重構。常見的參數順序約定包括:動作-物件、物件-動作、語意意義和遵循標準函式庫。最佳順序取決於函數目的、參數類型、潛在混淆和語言慣例。

1. SUM函數,用於對一列或一組單元格中的數字進行求和,例如:=SUM(A1:J10)。 2、AVERAGE函數,用於計算一列或一組儲存格中的數字的平均值,例如:=AVERAGE(A1:A10)。 3.COUNT函數,用於計算一列或一組單元格中的數字或文字的數量,例如:=COUNT(A1:A10)4、IF函數,用於根據指定的條件進行邏輯判斷,並返回相應的結果。

C++函數中預設參數的優點包括簡化呼叫、增強可讀性、避免錯誤。缺點是限制靈活性、命名限制。可變參數的優點包括無限彈性、動態綁定。缺點包括複雜性更高、隱式型別轉換、除錯困難。

C++中的函數傳回參考類型的好處包括:效能提升:引用傳遞避免了物件複製,從而節省了記憶體和時間。直接修改:呼叫方可以直接修改傳回的參考對象,而無需重新賦值。程式碼簡潔:引用傳遞簡化了程式碼,無需額外的賦值操作。

自訂PHP函數與預定義函數的差異在於:作用域:自訂函數僅限於其定義範圍,而預定義函數可在整個腳本中存取。定義方式:自訂函數使用function關鍵字定義,而預先定義函數則由PHP核心定義。參數傳遞:自訂函數接收參數,而預先定義函數可能不需要參數。擴充性:自訂函數可以根據需要創建,而預定義函數是內建的且無法修改。

C++中的異常處理可透過自訂異常類別增強,提供特定錯誤訊息、上下文資訊以及根據錯誤類型執行自訂操作。定義繼承自std::exception的異常類,提供特定的錯誤訊息。使用throw關鍵字拋出自訂異常。在try-catch區塊中使用dynamic_cast將捕獲到的異常轉換為自訂異常類型。在實戰案例中,open_file函數會拋出FileNotFoundException異常,捕捉並處理該異常可提供更具體的錯誤訊息。
