Searching: Chained Hash Table Search And Insertion-1

本文详细介绍了Chained Hash Table搜索与插入算法的实现过程,包括哈希函数、查找节点、插入节点等关键步骤,并通过Java程序展示了算法的具体应用。

Algorithm C

Algorithm C (Chained hash table search and insertion). This algorithm looks
for a given key K in an M-node table. If K is not in the table and the table is
not full, K is inserted.
The nodes of the table are denoted by TABLE[i], for 0 <= i <= M, and they
are of two distinguishable types, empty and occupied. An occupied node contains
a key field KEY[i], a link field LINK[i], and possibly other fields.
The algorithm makes use of a hash function h(K). An auxiliary variable
R is also used, to help find empty spaces; when the table is empty, we have
R = M + 1, and as insertions are made it will always be true that TABLE [j] is
occupied for all j in the range R <= j <= M. By convention, TABLE[0] will always
be empty.
C1. [Hash.] Set i <– h(K) + 1. (Now 1 <= i <= M.)
C2. [Is there a list?] If TABLE[i] is empty, go to C6. (Otherwise TABLE[i] is
occupied; we will look at the list of occupied nodes that starts here.)
C3. [Compare.] If K = KEY[i], the algorithm terminates successfully.
C4. [Advance to next.] If LINK[i] != 0, set i <– LINK[i] and go back to step C3.
C5. [Find empty node.] (The search was unsuccessful, and we want to find an
empty position in the table.) Decrease R one or more times until finding
a value such that TABLE[R] is empty. If R = 0, the algorithm terminates
with overflow (there are no empty nodes left); otherwise set LINK[i] <– R,
i <– R.
C6. [Insert new key.] Mark TABLE[i] as an occupied node, with KEY[i] <– K
and LINK[i] <– 0. |


Flow diagram

这里写图片描述


Coalesced chaining

这里写图片描述


Java program

In this program, R1,…,RN were simplified to K1,…,KN.

Node.java

package com.algorithms.searching;

/**
 * Created with IntelliJ IDEA.
 * User: 1O1O
 * Date: 12/13/13
 * Time: 6:52 PM
 * :)~
 * Chained Hash Table Search And Insertion:Searching
 */
public class Node {
    int KEY;
    int LINK;
    Node(int key){
        KEY = key;
        LINK=0;
    }
}

Main.java

package com.algorithms.searching;

/**
 * Created with IntelliJ IDEA.
 * User: 1O1O
 * Date: 12/13/13
 * Time: 6:52 PM
 * :)~
 * Chained Hash Table Search And Insertion:Searching
 */
public class Main {

    public static Node[] TABLE = new Node[20];
    public static int M = 19;
    public static int R = M+1;

    public static void searchAndInsertNode(int key){
        int hash = key%19+1;                  /*Hash function:h(K)*/

        if(TABLE[hash] == null){
            Node node = new Node(key);
            TABLE[hash] = node;
            System.out.println(String.format("%4s",key+":")+" not found in the hash table, then insert it in TABLE["+hash+"]");
        }else {
            do{
                if(key == TABLE[hash].KEY){
                    System.out.println(key+" found in the hash table: TABLE["+hash+"]");
                    break;
                }
                if(TABLE[hash].LINK != 0){
                    hash = TABLE[hash].LINK;
                }else {
                    do{
                        if(R == 0){
                            System.out.println("Error: Hash Table Overflow!"+", Key:"+key+" can not be inserted!");
                            break;
                        }else {
                            R--;
                            if(R!=0 && TABLE[R]==null){
                                TABLE[hash].LINK = R;
                                hash = R;
                                break;
                            }
                        }
                    }while (true);
                    if(R == 0){
                        break;
                    }
                    Node node = new Node(key);
                    TABLE[hash] = node;
                    System.out.println(String.format("%4s",key+":")+" not found in the hash table, then insert it in TABLE["+hash+"]");
                    break;
                }
            }while (true);
        }
    }

    public static void main(String[] args) {

        /*Prepare the Hash Table*/
        System.out.println("Prepare the Hash Table:");
        searchAndInsertNode(503);
        searchAndInsertNode(87);
        searchAndInsertNode(512);
        searchAndInsertNode(61);
        searchAndInsertNode(908);
        searchAndInsertNode(170);
        searchAndInsertNode(897);
        searchAndInsertNode(275);
        searchAndInsertNode(653);
        searchAndInsertNode(426);
        searchAndInsertNode(154);
        searchAndInsertNode(509);
        searchAndInsertNode(612);
        searchAndInsertNode(677);
        searchAndInsertNode(765);
        searchAndInsertNode(703);
        System.out.println();

        /*Print the current Hash Table with 16 nodes*/
        System.out.println("Print the current Hash Table with 16 nodes:");
        for(int j=1; j<=19; j++){
            if(TABLE[j] != null){
                System.out.println(String.format("%3s", j+":")+TABLE[j].KEY);
            }
        }
        System.out.println();

        /*Search Node 765 in current Hash Table*/
        System.out.println("Search Node 765 in current Hash Table:");
        searchAndInsertNode(765);
        System.out.println();

        searchAndInsertNode(1);
        searchAndInsertNode(2);
        searchAndInsertNode(3);
        searchAndInsertNode(4);
        searchAndInsertNode(5);
    }
}

Outputs

Prepare the Hash Table:
503: not found in the hash table, then insert it in TABLE[10]
 87: not found in the hash table, then insert it in TABLE[12]
512: not found in the hash table, then insert it in TABLE[19]
 61: not found in the hash table, then insert it in TABLE[5]
908: not found in the hash table, then insert it in TABLE[16]
170: not found in the hash table, then insert it in TABLE[18]
897: not found in the hash table, then insert it in TABLE[17]
275: not found in the hash table, then insert it in TABLE[15]
653: not found in the hash table, then insert it in TABLE[8]
426: not found in the hash table, then insert it in TABLE[9]
154: not found in the hash table, then insert it in TABLE[3]
509: not found in the hash table, then insert it in TABLE[14]
612: not found in the hash table, then insert it in TABLE[13]
677: not found in the hash table, then insert it in TABLE[11]
765: not found in the hash table, then insert it in TABLE[6]
703: not found in the hash table, then insert it in TABLE[1]

Print the current Hash Table with 16 nodes:
 1:703
 3:154
 5:61
 6:765
 8:653
 9:426
10:503
11:677
12:87
13:612
14:509
15:275
16:908
17:897
18:170
19:512

Search Node 765 in current Hash Table:
765 found in the hash table: TABLE[6]

  1: not found in the hash table, then insert it in TABLE[2]
  2: not found in the hash table, then insert it in TABLE[7]
  3: not found in the hash table, then insert it in TABLE[4]
Error: Hash Table Overflow!, Key:4 can not be inserted!
Error: Hash Table Overflow!, Key:5 can not be inserted!

Reference

<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH

这里有两段log,[2025-12-17 21:55:52] [2025-12-17 21:55:48] media_set_req_state():117 - [STREAM_COMMON][10.631][video] req state: connecting -> connected. [2025-12-17 21:55:52] [2025-12-17 21:55:48] clip_upload_thd_func():2521 - [STREAM_COMMON]get pre vu failed, clip start time: 1765979739 < vu utc: 1765979743 [2025-12-17 21:55:52] [2025-12-17 21:55:48] clip_upload_thd_func():2557 - [STREAM_COMMON]first vu pts: 4891993, vu utc: 1765979743, clip_startime: 1765979739. [2025-12-17 21:55:52] [2025-12-17 21:55:48] media_set_req_state():117 - [STREAM_COMMON][10.645][image] req state: send_req -> recv_rsp. [2025-12-17 21:55:52] [2025-12-17 21:55:48] common_recv_data_handle():2526 - [TapoCare]recv image err: 0 [2025-12-17 21:55:52] [2025-12-17 21:55:53] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979753, au: 1765979747 [2025-12-17 21:55:57] [2025-12-17 21:55:55] record_thread_func():4053 - [STM]video gop overlap, current start_time(1765979753) [2025-12-17 21:55:58] [2025-12-17 21:55:55] clip_manage_thd_func():959 - [clip]video frame overwritten,vb: 1765979755, ab: 1765979752 [2025-12-17 21:55:58] [2025-12-17 21:55:55] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979755, au: 1765979751 [2025-12-17 21:55:59] [2025-12-17 21:55:57] [ERROR] set_g_mp4_index_writed():1508 - [STM]set g_mp4_index_writed [1] [2025-12-17 21:56:00] [2025-12-17 21:55:57] record_thread_func():4786 - [STM]update index for event push sync [2025-12-17 21:56:00] [2025-12-17 21:55:57] SnapshotIndexMod_PostMsg():2243 - [SnapshotIdx]post msg(2) time(0). [2025-12-17 21:56:00] [2025-12-17 21:55:57] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [1] [2025-12-17 21:56:01] [2025-12-17 21:55:57] SnapshotIndex_Update():1271 - [SnapshotIdx]last_event(1765979640_1765979700_06) new_event(1765979739_1765979757_06) total(119) [2025-12-17 21:56:01] [2025-12-17 21:55:57] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [1] [2025-12-17 21:56:01] [2025-12-17 21:55:57] md_end_cb():379 - [AMS] MD end [2025-12-17 21:56:01] [2025-12-17 21:56:13] md_start_cb():367 - [AMS] MD start [2025-12-17 21:56:17] [2025-12-17 21:56:13] send_clip_manage_event_state():358 - [clip]send clip event[1765979739~1765979799], event_type=2, event_time=1765979773, duration=60 [2025-12-17 21:56:17] [2025-12-17 21:56:13] [ERROR] jpeg_time_callback():866 - [AVDC][36.005]jpeg update start, shm jpeg time: 492 -> 35892 [2025-12-17 21:56:17] [2025-12-17 21:56:13] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][36.006]jpeg update done, jpeg pts:35958717, shm_jpeg_time: 35892000. [2025-12-17 21:56:17] [2025-12-17 21:56:14] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[1], tm=72 [2025-12-17 21:56:17] [2025-12-17 21:56:14] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[1], tm=72 [2025-12-17 21:56:17] [2025-12-17 21:56:14] [ERROR] jpeg_time_callback():866 - [AVDC][36.163]jpeg update start, shm jpeg time: 35892 -> 10200719126691840 [2025-12-17 21:56:17] [2025-12-17 21:56:14] send_clip_manage_event_state():358 - [clip]send clip event[1765979739~1765979799], event_type=26, event_time=1765979774, duration=60 [2025-12-17 21:56:17] [2025-12-17 21:56:14] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][36.165]jpeg update done, jpeg pts:35958717, shm_jpeg_time: 10200719126691840000. [2025-12-17 21:56:17] [2025-12-17 21:56:14] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[1], tm=72 [2025-12-17 21:56:17] [2025-12-17 21:56:17] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979777, au: 1765979771 [2025-12-17 21:56:21] [2025-12-17 21:56:19] record_thread_func():4053 - [STM]video gop overlap, current start_time(1765979777) [2025-12-17 21:56:22] [2025-12-17 21:56:19] clip_manage_thd_func():959 - [clip]video frame overwritten,vb: 1765979779, ab: 1765979776 [2025-12-17 21:56:22] [2025-12-17 21:56:20] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979780, au: 1765979775 [2025-12-17 21:56:24] [2025-12-17 21:56:26] md_end_cb():379 - [AMS] MD end [2025-12-17 21:56:30] [2025-12-17 21:56:29] record_thread_func():4053 - [STM]video gop overlap, current start_time(1765979787) [2025-12-17 21:56:32] [2025-12-17 21:56:29] clip_manage_thd_func():865 - [clip]clip reached maximum recording duration, slient AMS to [1765979828]. cur block end_time:1765979789,record_buffer:10, clip start_time:1765979739, clip_length:60 [2025-12-17 21:56:32] [2025-12-17 21:56:29] clip_manage_thd_func():959 - [clip]video frame overwritten,vb: 1765979789, ab: 1765979786 [2025-12-17 21:56:32] [2025-12-17 21:56:30] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979790, au: 1765979785 [2025-12-17 21:56:33] [2025-12-17 21:56:37] SnapshotIndexMod_PostMsg():2243 - [SnapshotIdx]post msg(2) time(0). [2025-12-17 21:56:40] [2025-12-17 21:56:37] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [1] [2025-12-17 21:56:40] [2025-12-17 21:56:37] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [1] [2025-12-17 21:56:40] [2025-12-17 21:56:39] clip_manage_thd_func():904 - [clip]silent until 1765979829 [2025-12-17 21:56:42] [2025-12-17 21:56:39] clip_manage_thd_func():625 - [clip]clips stop, start ts:1765979739, end ts: 1765979799, clips last=60s. [2025-12-17 21:56:42] [2025-12-17 21:56:39] record_thread_func():4684 - [STM]video block reach endtime, stop clip, cur block[1765979797~1765979799], clip[1765979739~1765979799] [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] set_g_mp4_index_writed():1508 - [STM]set g_mp4_index_writed [0] [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [0] [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [0] [2025-12-17 21:56:42] [2025-12-17 21:56:39] DetClip_SetState():1400 - [SnapshotIdx] clip state: process -> idle [2025-12-17 21:56:42] [2025-12-17 21:56:39] SnapshotIndex_ClipEventEnd():1441 - [SnapshotIdx] clip end! [2025-12-17 21:56:42] [2025-12-17 21:56:39] record_thread_func():3764 - [STM]update index before change to pre_record [2025-12-17 21:56:42] [2025-12-17 21:56:39] record_thread_func():3768 - [STM]update event entry end status [2025-12-17 21:56:42] [2025-12-17 21:56:39] SnapshotIndexMod_PostMsg():2243 - [SnapshotIdx]post msg(2) time(0). [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] set_g_mp4_index_writed():1508 - [STM]set g_mp4_index_writed [1] [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [1] [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [1] [2025-12-17 21:56:42] [2025-12-17 21:56:39] dns_query_success_handle():778 - [tpssl][61.188]DNS use1-device-i-0f17d333cd00d93aa.tapo-care.i.tplinknbu.com --> 3.87.99.54 [2025-12-17 21:56:42] [2025-12-17 21:56:39] media_set_req_state():117 - [STREAM_COMMON][61.215][summary] req state: idle -> connecting. [2025-12-17 21:56:42] [2025-12-17 21:56:39] write_file_info_and_index_block():4246 - [STM]write index info to /tmp/mnt/harddisk_1/index_info.bin for [video] on disk 1 [2025-12-17 21:56:42] [2025-12-17 21:56:39] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[0], tm=62 [2025-12-17 21:56:43] [2025-12-17 21:56:39] tcp_connect_timeout_handle():438 - [tpssl][61.449]ip:3.87.99.54 tcp connecting --> tcp connected. [2025-12-17 21:56:43] [2025-12-17 21:56:39] tcp_connect_timeout_handle():508 - [tpssl][61.456]ip:3.87.99.54 tcp connected --> ssl connecting. [2025-12-17 21:56:43] [2025-12-17 21:56:40] tcp_recv_handle():346 - [tpssl][62.231]ip:3.87.99.54 ssl connecting --> ssl connected. [2025-12-17 21:56:43] [2025-12-17 21:56:40] media_set_req_state():117 - [STREAM_COMMON][62.255][summary] req state: connecting -> connected. [2025-12-17 21:56:43] [2025-12-17 21:56:40] media_set_req_state():117 - [STREAM_COMMON][62.289][summary] req state: connected -> send_req. [2025-12-17 21:56:44] [2025-12-17 21:56:40] media_set_req_state():117 - [STREAM_COMMON][62.782][summary] req state: send_req -> recv_rsp. [2025-12-17 21:56:44] [2025-12-17 21:56:40] common_recv_data_handle():2526 - [TapoCare]recv summary err: 0 [2025-12-17 21:56:44] [2025-12-17 21:56:41] pir_event_start_effect():1772 - [clip]pir verified, start clip [2025-12-17 21:56:45] [2025-12-17 21:56:41] start_clips_processing():1421 - [clip]silent until 1765979829 [2025-12-17 21:56:45] [2025-12-17 21:56:44] [ERROR] jpeg_time_callback():866 - [AVDC][66.163]jpeg update start, shm jpeg time: 10200719126691840 -> 10117156242980864 [2025-12-17 21:56:47] [2025-12-17 21:56:44] start_clips_processing():1421 - [clip]silent until 1765979829 [2025-12-17 21:56:47] [2025-12-17 21:56:44] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][66.165]jpeg update done, jpeg pts:65958687, shm_jpeg_time: 10117156242980864000. [2025-12-17 21:56:47] [2025-12-17 21:56:44] event_change_callback():2772 - [STM]clip not start, event->chn:0 [2025-12-17 21:56:47] [2025-12-17 21:56:47] [2025-12-17 21:56:44] pir_event_start_effect():1772 - [clip]pir verified, start clip [2025-12-17 21:56:48] [2025-12-17 21:56:44] start_clips_processing():1421 - [clip]silent until 1765979829 [2025-12-17 21:56:48] [2025-12-17 21:56:45] clip_upload_thd_func():2463 - [STREAM_COMMON]clip stop, start pts:4891993, end pts:60891993, current vu pts:60892027. [2025-12-17 21:56:49] [2025-12-17 21:56:49] [2025-12-17 21:56:45] clip_upload_thd_func():1968 - [STREAM_COMMON]clips upload stop, expected start ts:1765979739, expected end ts: 1765979799, cur time: 1765979805, expected duration=60s. [2025-12-17 21:56:49] [2025-12-17 21:56:45] clip_upload_thd_func():1970 - [STREAM_COMMON]clips upload stop, actual start ts:1765979743, actual end ts: 1765979799, event time: 1765979743, actual duration=56.000s.,第一段里actual start晚了4秒。第二个log是[2025-12-17 21:55:52] [2025-12-17 21:55:48] media_set_req_state():117 - [STREAM_COMMON][10.190][video] req state: connecting -> connected. [2025-12-17 21:55:52] [2025-12-17 21:55:48] clip_upload_thd_func():2521 - [STREAM_COMMON]get pre vu failed, clip start time: 1765979739 < vu utc: 1765979741 [2025-12-17 21:55:52] [2025-12-17 21:55:48] clip_upload_thd_func():2557 - [STREAM_COMMON]first vu pts: 2925074, vu utc: 1765979741, clip_startime: 1765979739. [2025-12-17 21:55:52] [2025-12-17 21:55:48] media_set_req_state():117 - [STREAM_COMMON][10.239][image] req state: connected -> send_req. [2025-12-17 21:55:52] [2025-12-17 21:55:48] link_status_update():476 - [NIFC]Link status: LINK_LINKING_UP -> LINK_UP [2025-12-17 21:55:52] [2025-12-17 21:55:48] link_status_update():478 - [NIFC]IP: ********, mask: 255.255.255.0, gateway: 192.168.0.1, DNS: 192.168.0.1, 0.0.0.0, conn_type:0 [2025-12-17 21:55:52] [2025-12-17 21:55:48] media_set_req_state():117 - [STREAM_COMMON][10.710][image] req state: send_req -> recv_rsp. [2025-12-17 21:55:52] [2025-12-17 21:55:48] common_recv_data_handle():2526 - [TapoCare]recv image err: 0 [2025-12-17 21:55:52] [2025-12-17 21:55:55] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979755, au: 1765979747 [2025-12-17 21:55:59] [2025-12-17 21:55:57] clip_manage_thd_func():952 - [clip]video frame overwritten,vb: 1765979757, ab: 1765979755 [2025-12-17 21:56:00] [2025-12-17 21:55:57] record_thread_func():4055 - [STM]video gop overlap, current start_time(1765979755) [2025-12-17 21:56:00] [2025-12-17 21:55:57] [ERROR] set_g_mp4_index_writed():1508 - [STM]set g_mp4_index_writed [1] [2025-12-17 21:56:01] [2025-12-17 21:55:57] record_thread_func():4782 - [STM]update index for event push sync [2025-12-17 21:56:01] [2025-12-17 21:55:57] SnapshotIndexMod_PostMsg():2249 - [SnapshotIdx]post msg(2) time(0). [2025-12-17 21:56:01] [2025-12-17 21:55:57] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [1] [2025-12-17 21:56:01] [2025-12-17 21:55:57] SnapshotIndex_Update():1271 - [SnapshotIdx]last_event(1765979640_1765979700_06) new_event(1765979739_1765979757_06) total(119) [2025-12-17 21:56:01] [2025-12-17 21:55:57] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [1] [2025-12-17 21:56:01] [2025-12-17 21:55:57] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979757, au: 1765979753 [2025-12-17 21:56:01] [2025-12-17 21:55:57] clip_upload_thd_func():2572 - [STREAM_COMMON]vu overwritten while searching, next frame type: P frame, vu: 1765979757 [2025-12-17 21:56:01] [2025-12-17 21:55:57] md_end_cb():379 - [AMS] MD end [2025-12-17 21:56:01] [2025-12-17 21:55:59] clip_upload_thd_func():2629 - [STREAM_COMMON]get I frame, vu utc: 1765979759, vu pts: 20925025. [2025-12-17 21:56:03] [2025-12-17 21:56:03] [2025-12-17 21:56:13] send_clip_manage_event_state():362 - [clip]send clip event[1765979739~1765979799], event_type=26, event_time=1765979773, duration=60 [2025-12-17 21:56:17] [2025-12-17 21:56:13] [ERROR] jpeg_time_callback():866 - [AVDC][36.010]jpeg update start, shm jpeg time: 2591 -> 5686124383043584 [2025-12-17 21:56:17] [2025-12-17 21:56:13] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][36.010]jpeg update done, jpeg pts:35858389, shm_jpeg_time: 5686124383043584000. [2025-12-17 21:56:17] [2025-12-17 21:56:14] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[1], tm=72 [2025-12-17 21:56:17] [2025-12-17 21:56:18] md_start_cb():367 - [AMS] MD start [2025-12-17 21:56:22] [2025-12-17 21:56:18] send_clip_manage_event_state():362 - [clip]send clip event[1765979739~1765979799], event_type=2, event_time=1765979778, duration=60 [2025-12-17 21:56:22] [2025-12-17 21:56:18] [ERROR] jpeg_time_callback():866 - [AVDC][40.808]jpeg update start, shm jpeg time: 5686124383043584 -> 40725 [2025-12-17 21:56:22] [2025-12-17 21:56:18] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][40.808]jpeg update done, jpeg pts:40525050, shm_jpeg_time: 40725000. [2025-12-17 21:56:22] [2025-12-17 21:56:18] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[1], tm=72 [2025-12-17 21:56:22] [2025-12-17 21:56:29] clip_manage_thd_func():857 - [clip]clip reached maximum recording duration, slient AMS to [1765979828]. cur block end_time:1765979789,record_buffer:10, clip start_time:1765979739, clip_length:60 [2025-12-17 21:56:33] [2025-12-17 21:56:31] md_end_cb():379 - [AMS] MD end [2025-12-17 21:56:35] [2025-12-17 21:56:37] SnapshotIndexMod_PostMsg():2249 - [SnapshotIdx]post msg(2) time(0). [2025-12-17 21:56:41] [2025-12-17 21:56:37] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [1] [2025-12-17 21:56:41] [2025-12-17 21:56:37] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [1] [2025-12-17 21:56:41] [2025-12-17 21:56:39] clip_manage_thd_func():897 - [clip]silent until 1765979829 [2025-12-17 21:56:42] [2025-12-17 21:56:39] clip_manage_thd_func():629 - [clip]clips stop, start ts:1765979739, end ts: 1765979799, clips last=60s. [2025-12-17 21:56:42] [2025-12-17 21:56:39] record_thread_func():4680 - [STM]video block reach endtime, stop clip, cur block[1765979797~1765979799], clip[1765979739~1765979799] [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] set_g_mp4_index_writed():1508 - [STM]set g_mp4_index_writed [0] [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [0] [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [0] [2025-12-17 21:56:43] [2025-12-17 21:56:39] DetClip_SetState():1400 - [SnapshotIdx] clip state: process -> idle [2025-12-17 21:56:43] [2025-12-17 21:56:39] SnapshotIndex_ClipEventEnd():1441 - [SnapshotIdx] clip end! [2025-12-17 21:56:43] [2025-12-17 21:56:39] record_thread_func():3766 - [STM]update index before change to pre_record [2025-12-17 21:56:43] [2025-12-17 21:56:39] record_thread_func():3770 - [STM]update event entry end status [2025-12-17 21:56:43] [2025-12-17 21:56:39] SnapshotIndexMod_PostMsg():2249 - [SnapshotIdx]post msg(2) time(0). [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] set_g_mp4_index_writed():1508 - [STM]set g_mp4_index_writed [1] [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] set_g_index_binary_writed():1516 - [STM]set g_index_binary_writed [1] [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] set_g_snapshotIndex_Synced():1524 - [STM]set g_snapshotIndex_Synced [1] [2025-12-17 21:56:43] [2025-12-17 21:56:39] write_file_info_and_index_block():4246 - [STM]write index info to /tmp/mnt/harddisk_1/index_info.bin for [video] on disk 1 [2025-12-17 21:56:43] [2025-12-17 21:56:39] [ERROR] send_sd_recording_power_down_timeout():1546 - [STM]set sd rec tm[0], tm=62 [2025-12-17 21:56:43] [2025-12-17 21:56:40] dns_query_success_handle():778 - [tpssl][62.053]DNS use1-device-i-03312408c217305d7.tapo-care.i.tplinknbu.com --> 107.21.90.35 [2025-12-17 21:56:43] [2025-12-17 21:56:40] media_set_req_state():117 - [STREAM_COMMON][62.054][summary] req state: idle -> connecting. [2025-12-17 21:56:43] [2025-12-17 21:56:40] tcp_connect_timeout_handle():438 - [tpssl][62.279]ip:107.21.90.35 tcp connecting --> tcp connected. [2025-12-17 21:56:44] [2025-12-17 21:56:40] tcp_connect_timeout_handle():508 - [tpssl][62.285]ip:107.21.90.35 tcp connected --> ssl connecting. [2025-12-17 21:56:44] [2025-12-17 21:56:41] tcp_recv_handle():346 - [tpssl][63.067]ip:107.21.90.35 ssl connecting --> ssl connected. [2025-12-17 21:56:44] [2025-12-17 21:56:41] media_set_req_state():117 - [STREAM_COMMON][63.100][summary] req state: connecting -> connected. [2025-12-17 21:56:44] [2025-12-17 21:56:41] media_set_req_state():117 - [STREAM_COMMON][63.139][summary] req state: connected -> send_req. [2025-12-17 21:56:44] [2025-12-17 21:56:41] [ERROR] jpeg_time_callback():866 - [AVDC][63.203]jpeg update start, shm jpeg time: 40725 -> 63125 [2025-12-17 21:56:45] [2025-12-17 21:56:41] md_start_cb():367 - [AMS] MD start [2025-12-17 21:56:45] [2025-12-17 21:56:41] start_clips_processing():1414 - [clip]silent until 1765979829 [2025-12-17 21:56:45] [2025-12-17 21:56:41] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][63.205]jpeg update done, jpeg pts:62858380, shm_jpeg_time: 63125000. [2025-12-17 21:56:45] [2025-12-17 21:56:41] event_change_callback():2772 - [STM]clip not start, event->chn:0 [2025-12-17 21:56:45] [2025-12-17 21:56:45] [2025-12-17 21:56:41] pir_event_start_effect():1777 - [clip]pir verified, start clip [2025-12-17 21:56:45] [2025-12-17 21:56:41] start_clips_processing():1414 - [clip]silent until 1765979829 [2025-12-17 21:56:45] [2025-12-17 21:56:41] start_clips_processing():1414 - [clip]silent until 1765979829 [2025-12-17 21:56:45] [2025-12-17 21:56:41] media_set_req_state():117 - [STREAM_COMMON][63.632][summary] req state: send_req -> recv_rsp. [2025-12-17 21:56:45] [2025-12-17 21:56:41] common_recv_data_handle():2526 - [TapoCare]recv summary err: 0 [2025-12-17 21:56:45] [2025-12-17 21:56:43] [ERROR] jpeg_time_callback():866 - [AVDC][65.969]jpeg update start, shm jpeg time: 63125 -> 5672380487696384 [2025-12-17 21:56:47] [2025-12-17 21:56:43] start_clips_processing():1414 - [clip]silent until 1765979829 [2025-12-17 21:56:47] [2025-12-17 21:56:43] event_change_callback():2772 - [STM]clip not start, event->chn:0 [2025-12-17 21:56:47] [2025-12-17 21:56:47] [2025-12-17 21:56:43] [ERROR] jpeg_sync_shm_loop():834 - [AVDC][65.971]jpeg update done, jpeg pts:65858389, shm_jpeg_time: 5672380487696384000. [2025-12-17 21:56:47] [2025-12-17 21:56:44] pir_event_start_effect():1777 - [clip]pir verified, start clip [2025-12-17 21:56:48] [2025-12-17 21:56:44] start_clips_processing():1414 - [clip]silent until 1765979829 [2025-12-17 21:56:48] [2025-12-17 21:56:44] start_clips_processing():1414 - [clip]silent until 1765979829 [2025-12-17 21:56:48] [2025-12-17 21:56:45] clip_upload_thd_func():2825 - [STREAM_COMMON]video frame overwritten, next frame type: P frame, vu: 1765979805, au: 1765979797 [2025-12-17 21:56:49] [2025-12-17 21:56:45] clip_upload_thd_func():2600 - [STREAM_COMMON]clip stop after vu overwritten, start pts:2925074, end pts:60925074, current vu pts:62925029. [2025-12-17 21:56:49] [2025-12-17 21:56:49] [2025-12-17 21:56:45] clip_upload_thd_func():1968 - [STREAM_COMMON]clips upload stop, expected start ts:1765979739, expected end ts: 1765979799, cur time: 1765979805, expected duration=60s. [2025-12-17 21:56:49] [2025-12-17 21:56:45] clip_upload_thd_func():1970 - [STREAM_COMMON]clips upload stop, actual start ts:1765979741, actual end ts: 1765979797, event time: 1765979743, actual duration=55.933s.,第二段里actual start晚了2s,但是actual end也少了2秒。请对比分析一下为什么提前结束的原因
最新发布
12-19
我们来详细对比分析这两段日志,重点关注两个关键点: 1. **实际开始时间(actual start)延迟**:为什么视频上传的实际开始时间比预期晚? 2. **实际结束时间提前**:为什么第一段日志中 clip 的实际结束是 `1765979799`,而第二段却只到 `1765979797`?即提前了 2 秒。 --- ## 🔍 日志背景说明 - 所有时间戳单位为 **秒级 UTC 时间**(如 `1765979739` 是 Unix 时间戳) - 每个事件期望录制 **60 秒**,从 `1765979739` 到 `1765979799` - `vu`: 视频时间戳(video UTC timestamp),用于定位帧 - `pts`: 帧的时间戳(Presentation Time Stamp) - `clip_upload_thd_func()` 负责上传剪辑视频 - `get pre vu failed` 表示无法在缓冲区找到早于目标时间的帧(起始 I 帧缺失) - `video frame overwritten` 表示环形缓冲区中的视频帧被覆盖,可能导致数据丢失 - `silent until X` 表示系统暂时不处理新事件,防止重复触发 --- ## ✅ 第一段日志分析(正常上传,但 start 晚 4s) ### 关键信息: ```log [...] get pre vu failed, clip start time: 1765979739 < vu utc: 1765979743 [...] first vu pts: 4891993, vu utc: 1765979743, clip_startime: 1765979739. ``` ➡️ 实际开始时间:`1765979743` ➡️ 预期开始时间:`1765979739` ✅ **延迟了 4 秒** ### 原因: 设备在尝试获取 `1765979739` 之前的视频帧时失败(`get pre vu failed`),说明此时环形缓冲区中已经没有足够早的数据。最早的可用帧时间是 `1765979743`,所以只能从这里开始读取。 这通常是因为: - 环形缓冲区太小(例如仅保留最近 30 秒) - 事件触发后,上传线程启动较慢 - 缓冲区写入和读取不同步 但是最终上传完成,并且: ```log clips upload stop, actual start ts:1765979743, actual end ts: 1765979799, ... actual duration=56.000s. ``` ➡️ 结束时间正确达到了 `1765979799`,只是开头丢了 4 秒。 📌 **结论一**:虽然起始偏移,但由于后续帧完整,仍能录满 60 秒事件窗口。 --- ## ❗ 第二段日志分析(start 晚 2s,end 提前 2s) ### 关键信息: ```log get pre vu failed, clip start time: 1765979739 < vu utc: 1765979741 first vu pts: 2925074, vu utc: 1765979741, clip_startime: 1765979739. ... clips upload stop, actual start ts:1765979741, actual end ts: 1765979797, ... actual duration=55.933s. ``` ➡️ 实际开始:`1765979741`(+2s) ➡️ 实际结束:`1765979797`(-2s) ⚠️ **总共少了 4 秒内容** 更严重的是,它本应录到 `1765979799`,结果只录到了 `1765979797`,即提前结束了 2 秒。 --- ## 🔎 对比两段日志的关键差异 | 项目 | 第一段 | 第二段 | |------|--------|--------| | 最早可得视频帧 (`vu`) | 1765979743 | 1765979741 | | 实际开始时间 | 1765979743 | 1765979741 | | 实际结束时间 | 1765979799 ✅ | 1765979797 ⚠️ | | 是否出现 `frame overwritten` | 多次出现,但在上传期间持续收到 P 帧 | 同样多次出现 | | 是否成功上传全部? | 是(直到 1765979799) | 否(止于 1765979797) | --- ## 🚩 根本原因分析:为何第二段提前结束? ### 关键线索来自以下日志行: ```log [2025-12-17 21:56:29] clip_manage_thd_func():857 - [clip]clip reached maximum recording duration, slient AMS to [1765979828]. cur block end_time:1765979789, record_buffer:10, clip start_time:1765979739, clip_length:60 ``` ⚠️ 这个日志非常关键! #### 解读: - 当前块(current block)的 `end_time` 是 `1765979789` - 但系统判断“已达到最大录制时长”,于是提前终止了 clip 录制 - 尽管目标是 `1765979799`,但它在 `1765979789` 就停止追加数据 这意味着什么? 👉 **环形缓冲区的当前记录块(block)只有 10 秒长度?** 注意这个字段:`record_buffer:10` 推测:设备使用分块管理视频缓存,每块可能是 10 秒。如果 clip 涉及多个 block,需要跨块拼接。 但在第二段中,当系统试图读取超过当前块范围的内容时,可能因为: - 下一块尚未生成或未标记为 ready - 或者由于 `video frame overwritten` 导致下一帧不可用 - 或者上传线程误判“已达最大时长”而中断 再看上传线程的日志: ```log clip_upload_thd_func():2600 - [STREAM_COMMON]clip stop after vu overwritten, ... ``` 🚨 出现关键词:`clip stop after vu overwritten` 这表明:**在上传过程中检测到视频帧被覆盖,因此主动终止了上传流程**,担心数据不连续。 而在第一段日志中虽然也有 `video frame overwritten`,但上传线程仍然继续运行并完成了上传。 --- ## ✅ 综合对比与根本原因总结 | 因素 | 第一段 | 第二段 | 差异影响 | |------|--------|--------|---------| | 可用最早帧 | 1765979743 | 1765979741 | 第二段反而更早,不是问题根源 | | 是否发生帧覆盖 | 是 | 是 | 都发生,但第二段导致上传中断 | | 是否因 `vu overwritten` 停止上传 | ❌ 否 | ✅ 是(`clip stop after vu overwritten`) | | 是否达到目标结束时间 | ✅ 是 | ❌ 否(止于 1765979797) | | 是否静默 AMS | ✅ 是(silent until 1765979829) | ✅ 是 | | 缓冲区状态 | 可能较稳定 | 可能正在切换 block 或写入异常 | 第二段处于不稳定状态 | --- ## 🔧 根本原因结论 > **第二段日志中 clip 提前结束的根本原因是:在上传过程中发生了 `video frame overwritten`,导致上传线程调用 `clip stop after vu overwritten` 主动终止了上传流程,未能等到完整的 60 秒数据。** 相比之下,第一段虽然也存在帧覆盖警告,但上传线程没有立即中断,而是继续读取直到目标时间 `1765979799`,因此能上传更完整的内容。 此外,第二段中: - 系统提前判定“达到最大录制时长”(尽管才 50 秒左右) - 可能由于 `record_buffer` 分块机制缺陷,导致无法跨块读取 - 加上帧覆盖引发的安全退出策略,共同导致提前结束 --- ## 💡 改进建议(代码层面) 为了提升稳定性,建议修改上传逻辑,避免因单次 `frame overwritten` 就中断整个上传: ```c // 示例伪代码:clip_upload_thd_func 中对 overwrite 的处理优化 while (uploading) { frame = get_next_video_frame(); if (!frame) { log_warning("video frame missing or overwritten, vu: %u", current_vu); // ❌ 错误做法:直接 break; // break; // ✅ 正确做法:跳过丢失帧,继续尝试后续帧 current_vu += FRAME_INTERVAL; continue; } if (frame->timestamp >= expected_end_time) { finalize_clip_upload(); break; } write_frame_to_clip(frame); } ``` 同时确保: - 环形缓冲区大小 ≥ 60 秒(推荐 90~120 秒以应对延迟) - 使用可靠的 I 帧查找机制(基于 keyframe index) - 添加重试机制:若某段时间无数据,等待短暂时间再查一次共享内存 --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值