Magpie: Garbage Collection chính xác cho C - Luận án tiến sĩ

Luận án tiến sĩ Magpie về garbage collection chính xác cho C. Hệ thống tự động chuyển đổi quản lý bộ nhớ, giảm lỗi và cải thiện hiệu năng chương trình.

Trường ĐH

The University of Utah

Chuyên ngành

Computer Science

Tác giả

Luan An

Thể loại

dissertation

Năm xuất bản

Số trang

139

Thời gian đọc

21 phút

Lượt xem

0

Lượt tải

0

Phí lưu trữ

40 Point

Tóm tắt nội dung

I. Garbage Collection chính xác cho ngôn ngữ C

Magpie mang garbage collection chính xác đến với ngôn ngữ C. Hệ thống này giải quyết vấn đề quản lý bộ nhớ tự động - một thách thức lớn trong lập trình C. C và C++ cung cấp tốc độ nhanh và tính linh hoạt cao. Nhưng sự phức tạp trong memory management là điểm yếu. Lập trình viên phải tạo và hủy đối tượng thủ công. Sai sót nhỏ gây hậu quả nghiêm trọng. Các ngôn ngữ khác sử dụng precise garbage collection để máy tính tự quản lý bộ nhớ. Trước Magpie, việc thêm thu gom rác chính xác vào chương trình C chuẩn tốn nhiều công sức. Luận án này mô tả Magpie - hệ thống sử dụng nhiều phân tích và kỹ thuật chuyển đổi để giảm gánh nặng này.

1.1. Vấn đề quản lý bộ nhớ trong C

C và C++ yêu cầu lập trình viên quản lý heap memory thủ công. Phải gọi malloc() để cấp phát và free() để giải phóng. Quên giải phóng bộ nhớ tạo memory leak. Giải phóng sớm gây lỗi truy cập không hợp lệ. Giải phóng hai lần làm hỏng cấu trúc dữ liệu. Những lỗi này khó phát hiện và sửa chữa.

1.2. Ưu điểm của automatic memory management

Garbage collection tự động loại bỏ các lỗi quản lý bộ nhớ. Conservative garbage collector đã tồn tại cho C nhưng kém hiệu quả. Precise garbage collection biết chính xác con trỏ nào còn sử dụng. Điều này cho phép thu gom rác hiệu quả hơn và di chuyển đối tượng trong bộ nhớ.

1.3. Mục tiêu của Magpie

Magpie tự động chuyển đổi mã C để sử dụng precise garbage collection. Hệ thống phân tích mã nguồn và thêm thông tin cần thiết. Giảm công việc thủ công xuống mức tối thiểu. Cho phép tận dụng cơ sở mã C hiện có với memory management hiện đại.

II. Thiết kế tổng quan của hệ thống Magpie

Magpie sử dụng kiến trúc nhiều giai đoạn để chuyển đổi mã C. Hệ thống phân tích cấu trúc dữ liệu, điểm cấp phát, và call graph. Sau đó tạo garbage collector tùy chỉnh cho từng chương trình. Quá trình này tự động hóa hầu hết công việc chuyển đổi. Magpie xử lý cả mã nguồn và thư viện liên kết. Thiết kế cho phép điều chỉnh thông qua cờ trong mã nguồn. Kết quả là chương trình C với automatic memory management đầy đủ chức năng.

2.1. Cơ chế hoạt động của garbage collection

Garbage collector theo dõi tất cả đối tượng được cấp phát. Quét từ root set - biến toàn cục và stack. Đánh dấu mọi đối tượng có thể truy cập được. Thu hồi bộ nhớ của đối tượng không được đánh dấu. Precise collection cần biết chính xác vị trí con trỏ trong mỗi đối tượng.

2.2. Kiến trúc đa giai đoạn của Magpie

Giai đoạn đầu phân tích cấu trúc dữ liệu. Xác định các trường là con trỏ trong struct và union. Giai đoạn hai phân tích điểm cấp phát bộ nhớ. Giai đoạn ba xây dựng call graph. Giai đoạn cuối tạo garbage collector và chuyển đổi mã.

2.3. Xử lý thư viện và mã bên ngoài

Magpie xử lý cả mã nguồn có sẵn và thư viện biên dịch sẵn. Với thư viện, sử dụng wrapper functions. Cờ trong mã nguồn cho phép lập trình viên gợi ý về hành vi. Hệ thống tự động phát hiện hầu hết trường hợp nhưng cho phép can thiệp thủ công khi cần.

III. Phân tích cấu trúc và allocation trong C

Magpie thực hiện nhiều phân tích tĩnh trên mã nguồn C. Phân tích cấu trúc xác định layout của struct và union. Phân tích allocation tìm mọi điểm cấp phát bộ nhớ. Call graph analysis theo dõi luồng điều khiển chương trình. Các phân tích này cung cấp thông tin cần thiết cho precise garbage collection. Magpie tạo traversal routines cho mỗi kiểu dữ liệu. Routines này cho phép garbage collector quét đối tượng chính xác.

3.1. Phân tích cấu trúc dữ liệu

Hệ thống quét định nghĩa struct và union. Xác định trường nào là con trỏ, số nguyên, hay kiểu khác. Xây dựng bản đồ layout cho mỗi kiểu. Xử lý cấu trúc lồng nhau và mảng. Thông tin này quan trọng để garbage collector biết nơi tìm con trỏ.

3.2. Phát hiện điểm cấp phát bộ nhớ

Magpie tìm mọi lời gọi malloc, calloc, realloc. Phân tích kích thước được cấp phát. Xác định kiểu dữ liệu được tạo tại mỗi điểm. Chuyển đổi sang lời gọi garbage collector tương ứng. Thêm thông tin kiểu để collector biết cách quét đối tượng.

3.3. Xây dựng call graph

Call graph analysis theo dõi hàm nào gọi hàm nào. Thông tin này cần thiết để xác định root set. Giúp tối ưu hóa việc quét stack. Xác định điểm cần lưu trữ thông tin stack frame. Call graph cũng hỗ trợ phân tích escape của đối tượng.

IV. Chuyển đổi stack và tạo garbage collector

Magpie chuyển đổi stack để garbage collector có thể quét chính xác. Thêm stack frames chứa thông tin về biến cục bộ. Mỗi frame mô tả shape của biến - trường nào là con trỏ. Hệ thống tạo mã để đẩy và pop frames khi vào/ra hàm. Xử lý cả mảng và tagged values. Garbage collector được tạo tùy chỉnh cho từng chương trình. Sử dụng traversal routines đã tạo để quét đối tượng. Hỗ trợ cả single-threaded và multi-threaded programs.

4.1. Cơ chế stack conversion

Mỗi hàm được thêm mã để tạo stack frame. Frame chứa danh sách biến cục bộ và kiểu của chúng. Khi garbage collection chạy, quét tất cả frames trên stack. Tìm con trỏ trong biến cục bộ và đánh dấu đối tượng tương ứng. Stack conversion là phần phức tạp nhất của Magpie.

4.2. Tạo garbage collector tùy chỉnh

Magpie tạo collector dựa trên thông tin đã phân tích. Bao gồm traversal routines cho mỗi kiểu dữ liệu. Collector biết chính xác cách quét mỗi đối tượng. Tối ưu hóa dựa trên đặc điểm của chương trình cụ thể. Hiệu quả hơn generic conservative collector.

4.3. Hỗ trợ đa luồng

Magpie xử lý chương trình multi-threaded. Đồng bộ hóa khi garbage collection chạy. Dừng tất cả threads để quét bộ nhớ an toàn. Mỗi thread có stack riêng cần được quét. Thêm overhead nhưng đảm bảo tính đúng đắn.

V. Chi phí và hiệu năng của conversion

Luận án đánh giá Magpie trên nhiều benchmark programs. So sánh với phiên bản gốc và Boehm conservative collector. Đo thời gian chạy, sử dụng bộ nhớ, và chi phí chuyển đổi. Kết quả cho thấy overhead chấp nhận được. Một số chương trình chạy nhanh hơn nhờ locality tốt hơn. Magpie xử lý unions và cấu trúc phức tạp trong benchmarks. Phân tích chi tiết từng giai đoạn: Base, NoGC, NoOp.

5.1. Tổng quan các benchmark programs

Sử dụng nhiều chương trình thực tế để kiểm tra. Bao gồm ứng dụng có cơ sở mã lớn. Chương trình với allocation patterns khác nhau. Một số có unions và cấu trúc dữ liệu phức tạp. Benchmarks đại diện cho use cases thực tế của C.

5.2. So sánh với Boehm collector

Boehm là conservative garbage collector phổ biến cho C. Không cần chuyển đổi mã nhưng kém chính xác. Magpie precise hơn nên thu gom hiệu quả hơn. So sánh thời gian chạy và memory footprint. Magpie có overhead khởi động cao hơn nhưng runtime tốt hơn trong nhiều trường hợp.

5.3. Phân tích chi phí thời gian

So sánh Base (không GC) với NoGC (đã convert nhưng không chạy GC). NoGC với NoOp (GC chạy nhưng không thu gom). Cuối cùng với Full GC (hoạt động đầy đủ). Tách biệt overhead của conversion và garbage collection. Kết quả cho thấy conversion overhead thấp, GC overhead phụ thuộc vào allocation rate.

VI. Ứng dụng mở rộng Memory accounting

Cơ sở hạ tầng garbage collection cho phép nhiều ứng dụng khác. Debugging tools có thể theo dõi allocation patterns. Language runtimes tận dụng precise collection. Memory accounting là một ứng dụng quan trọng. Theo dõi bộ nhớ sử dụng bởi từng phần của chương trình. Xác định memory leaks và inefficiencies. Giúp tối ưu hóa sử dụng bộ nhớ. Luận án mô tả chi tiết memory accounting system.

6.1. Công cụ debugging nâng cao

Precise GC infrastructure hỗ trợ debugging tốt hơn. Theo dõi mọi đối tượng và quan hệ giữa chúng. Phát hiện dangling pointers và use-after-free. Visualize object graphs và memory layout. Debugging tools mạnh mẽ hơn nhiều so với manual memory management.

6.2. Memory accounting chi tiết

Hệ thống theo dõi bộ nhớ của từng module hoặc subsystem. Biết đối tượng nào thuộc phần nào của chương trình. Báo cáo sử dụng bộ nhớ theo thời gian. Phát hiện memory bloat và optimization opportunities. Memory accounting giúp hiểu rõ hành vi bộ nhớ của ứng dụng lớn.

6.3. Tích hợp với language runtimes

Language runtimes cho ngôn ngữ scripting có thể sử dụng Magpie. Cung cấp precise collection cho C code trong runtime. Tích hợp với GC của ngôn ngữ cấp cao. Cho phép interoperability tốt hơn. Mở rộng khả năng của cả C và ngôn ngữ được nhúng.

Xem trước tài liệu
Tải đầy đủ để xem toàn bộ nội dung
Luận án tiến sĩ: Magpie: Precise garbage collection for C

Tải xuống file đầy đủ để xem toàn bộ nội dung

Tải đầy đủ (139 trang)

Trích đoạn nội dung luận án

Tải xuống để đọc toàn bộ

MAGPIE: PRECISE GARBAGE COLLECTION FOR C by Adam Wick A dissertation submitted to the faculty of The University of Utah in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science School of Computing The University of Utah December 2006 UMI Number: 3241833 Copyright 2006 by Wick, Adam All rights reserved. INFORMATION TO USERS The quality of this reproduction is dependent upon the quality of the copy submitted. Broken or indistinct print, colored or poor quality illustrations and photographs, print bleed-through, substandard margins, and improper alignment can adversely affect reproduction. In the unlikely event that the author did not send a complete manuscript and there are missing pages, these will be noted.

Also, if unauthorized copyright material had to be removed, a note will indicate the deletion. ® UMI UMI Microform 3241833 Copyright 2007 by ProQuest Information and Learning Company. All rights reserved. This microform edition is protected against unauthorized copying under Title 17, United States Code.

ProQuest Information and Learning Company 300 North Zeeb Road P. Box 1346 Ann Arbor, MI 48106-1346 Copyright © Adam Wick 2006 All Rights Reserved THE UNIVERSITY OF UTAH GRADUATE SCHOOL SUPERVISORY COMMITTEE APPROVAL of a dissertation submitted by Adam Wick This dissertation has been read by each member of the following supervisory committee and by majority vote has been found to be satisfactory. Chair: 2— MatthewF latt alloc Lé LfOL [ ' hyilson Tei’ V 3/2 l2s Ta. Kent Dybvig a THE UNIVERSITY OF UTAH GRADUATE SCHOOL FINAL READING APPROVAL To the Graduate Council of the University of Utah: I have read the dissertation of Adam Wick in its final form and have found that (1) its format, citations, and bibliographic style are consistent and acceptable; (2) its illustrative materials including figures, tables, and charts are in place; and (3) the final manuscript is satisfactory to the Supervisory Committee and is ready for submission to The Graduate School.

7[1 (06 Z2[keer Date Matthew Flatt Chair, Supervisory Committee Approved for the Major Department M. bey Martin Berzins Chair /Dean Approved for the Graduate Council a ee he Oe David S. Chapman ` Dean of The Graduate School ABSTRACT C and C++ provide fast, flexible substrata for programs requiring speed or tight coupling with the operating system or hardware. Both languages have well established user and code bases, including programs still in use after decades of development.

Unfortunately, with C and C+-+’s speed and flexibility come increased complexity, including complication in managing memory. Programs must create and destroy objects explicitly, and small mistakes in doing so can cause severe complications. In other languages, precise garbage collection solves these problems by having the computer manage the program’s memory. However, until now, adding precise garbage collection to standard C programs has been a considerable amount of work.

This dissertation describes Magpie, a system that uses several analyses and conversion techniques to relieve much of the burden of this conversion. It also describes the effects of the conversion on several sample programs. Finally, debugging tools and language runtimes can perform additional inter- esting tasks given an existing garbage collection infrastructure. This dissertation outlines several such extensions, and discusses one —- memory accounting — in detail.

iv LIST OF FIGURES. eee viii LIST OF LABLES. PRECISE COLLECTION AND C PROGRAMS .1 Memory Management Paradigms.1 Static Allocation / No Deallocation.2 Manual Memory Management.3 Compilers and Garbage Collection. THE HIGH LEVEL DESIGN OF MACPIE.1 Co: ee eee eee eens 13 2.2 The Mechanics of Garbage Collection .1 The Design of Magpie .2 Dealing with Libraries.3 In-Source Flags .3 Limitations of Magpie vs.1 Limitations of Magpie.2 Comparisons to Boehm.1 Generating the Input.

ccc eee eens 25 3. eee eee và 30 3.4 Call Graph Analysis.5 Garbage Collector Generatlon.caaIáaáaaa eee eee eens 38 IMPLEMENTING MAGPIE.1 Implementing the Allocation Analysis.1 Gathering the Allocation Analysis Information.2 Converting the Allocation Points.2 Implementing the Structure Analysis.1 Coping with Too Many Structures.2 Creating the Traversal Routines.3 Implementing the Call Graph AnalÌysis.4 Implementing the Stack Conversion.1 Overview of the Stack Conversion.2 Internal Variable Shape Forms .3 Caveats Regarding Optimizations.4 Adding Stack Frames.2 Array and Tagged Saves. cc eee ees 61 4.5 Removing Stack FYames. ccc eee eens 63 4.6 Dealing with Shared Libraries .7 Implementing the Garbage Collector.

cc eee eee 66 4.3 Tuning the Garbage Collector .8 Threads and Magpie. eee eens 68 THE COST OF CONVERSION.1 An Overview of the Benchmarks.2 Converting the Benchmarks.1 Using Boehm with the Benchmarks. ce eee eens 73 5.3 Unions in the Benchmarks. ee eee tees 78 5.3 The Cost in Time .1 Comparing Base and NoGC .2 Comparing NoGC and NoÓp£_.3 Comparing NoOpt and MÍagpie.4 The Cost of ÂutOfAaBgBÌnE.5 Comparing Base, Boehm and Mlagpie.7 Possible Shadow Variable Ôptimization.0 Final Discussions on Space and Time .1 Object Deallocation Costs.3 Smaller Object SlZ@§.

ceeee 96 EXPLOITING PRECISE GC: MEMORY ACCOUNTING .2 Assignment Hand-In Server .2 Consumer-Based Âccounting.3 Accounting in the Examples .2 Hand-In Server. eee nee eens 106 6.00 eee eee eee 106 6.2 Vertically Communicating Processes.3 Horizontally Communicating Processes.4 Libraries and Callbacks.5 Producer-Based Âccounting. cc c eee eens 110 6. kg ng kh nh eee vy và 113 6.6 Comparisons to Existing Systems.1 Magpie for C/VM Inierfaces.

cee eee eens 118 7. 0 0 ccc eee een ees 123 vii LIST OF FIGURES 1.1 A screenshot of Apple’s Safari web browser using nearly 3 gigabytes of memory after a couple of hours of normal usage.1 The high-level design of the Magpie toolset.1 The allocation analysis window for the top source file libtop.2 An example of the allocation analysis window where the object in question is a tagged objeCf.3 The structure analysis window for the top source file libtop.4 The difference between (a) an array of struct foos and (b) an array of pointers to struct foos. The latter case is considerably more common in ĐTAGẨIC€.0 eee ee ee 32 3.9 An example of entering in the information for the “an array of inline objects, of size” case. Note that the field in question does not, in fact, declare such a thing; this figure is merely an example of the information needed in these cases.

eee ee eee 33 3.6 An example of entering in a custom traversal function. Again, this is a fictitious example; there is no reason to write a traverser for this field.7 The structure analysis GUI for unions.1 Exemplars of the four kinds of shadow stack frames in Magpie.1 The memory behavior of the 164.2 The memory behavior of the 175.3 The memory behavior of the 176.4 The memory behavior of the 179.5 The memory behavior of the 181.6 The memory behavior of the 183.7 The memory behavior of the 186.8 The memory behavior of the 188.9 The memory behavior of the 197.10 The memory behavior of the 254.11 The memory behavior of the 256.12 The memory behavior of the 300.1 The three interprocess communication patterns. The filled circle rep- resents the parent process, with the hollow circles representing child processes. The arrows represent directions of communication.2 The four steps of the accounting procedure.3 A potential heap layout, mid-collection.

The grayed objects have been marked by the collector. cece eee eee ix LIST OF TABLES 5.1 An overview of the size of the various benchmarks used. All prepro- cessed files generated on Mac OS/X 10.2 The cost of the allocation analysis for each of the benchmark pro- grams. Parse time is the time spent in parsing and massaging the source into the correct internal formats.

User time is the amount of time the programmer spends answering questions. All times approxi- Mate, 2. ee nee eee eae 74 5.3 The cost of the structure analysis for each of the benchmark programs. Parse time is the time spend in parsing and massaging the source in the correct internal formats.

User time is the amount of time the programmer spends answering questions. All times approximate.4 The cost of the automatic conversions. Conversion time is the time spent by Magpie in the various analyses, transformations and addi- tions required to take the original file and create the internal repre- sentation of the converted file. Total convert time includes parsing, unparsing and recompilation of the file.5 The number of unions in each of the benchmark programs, and how they are handled for the conversion.6 The impact of the Magpie conversion on executable sizes.7 The performance impact of garbage collection on the benchmarks.

80 CHAPTER 1 PRECISE COLLECTION AND C PROGRAMS Memory management is one of the most tedious and error-prone tasks in soft- ware development. Small, unnoticed memory-management mistakes can cause crashes, security problems, slow degradation of program performance, and OS crashes. While testing catches many of these errors, some remain even in released software.1 for an example of Apple’s Safari web browser afflicted with a slow memory leak. Reliance on legacy code exacerbates the memory-management problem.

Many companies and institutions rely on programs they have used for decades; programs that have been modified by many different hands as managers add new requirements and users find new bugs. Often, the original programmer(s) for the application have moved to other companies, and program maintenance is left to people unfamiliar with the program’s design. Worse, documentation on the program’s design and implementation is usually either out of date or nonexistent, particularly with regard to memory management conventions. A programmer new to the project may need to spend weeks or months to find and correctly fix memory errors.

Rewriting legacy programs is often impractical or unwise; redeveloping a com- plex system using modern languages, tools, and designs may take years. Further, the redesign will introduce new bugs to be tracked and fixed. If the original program is critical for the company or institution, spending years to correct one problem — only to potentially introduce different problems — is an expensive risk with minimal hope of reward. The subject of this dissertation, Magpie, is one solution to this problem.

Magpie solves much of the problem of memory-management bugs by modifying a program threa liên: 4 Tà Figure 1. A screenshot of Apple’s Safari web browser using nearly 3 gigabytes of memory after a couple of hours of normal usage. to use garbage collection. Thus, the converted program automatically performs its own memory management, rather than relying on the programmer to get everything correct.

Thesis: Precise garbage collection offers advantages to programmers over manual memory management, through ease of programming, a lessening of memory errors, and increased tool support. Furthermore, these advantages are available for typical C-implemented programs with proper tool support. A tool can simplify the process of converting existing code to use precise collection, bringing these advantages to normal C programmers. Magpie is a tool to demonstrate this thesis.

Magpie contrasts with existing tools to aid in detecting memory errors in existing programs. These tools run the gamut from academic type systems, reworking of language runtimes, dynamic checking tools and complicated software analyses. This chapter continues with a survey of the subject of memory management, and the problems inherent with each memory management strategy, with the conclusion that precise garbage collection is often the best solution. It then outlines the contributions of this dissertation, and concludes with a roadmap for the remainder of the dissertation.1 Memory Management Paradigms Most programs manage memory using one or more of four basic memory- management strategies: static allocation, manual memory management, reference counting and garbage collection.

Each of the basic strategies has advantages and disadvantages with regard to space utilization and performance. Most have tool sets associated with them to aid in their adoption or in their use. This section examines each of these four basic strategies.1 Static Allocation / No Deallocation In some basic programs, very little memory is used; either no memory is al- located, or there is no need to deallocate any memory allocated. Simple student exercises, some simple command-line utilities, and even some more complex utilities (such as compression utilities) may fall under this category.

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ

Từ khóa và chủ đề nghiên cứu


Câu hỏi thường gặp

Luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" nghiên cứu về vấn đề gì?

Luận án tiến sĩ Magpie về garbage collection chính xác cho C. Hệ thống tự động chuyển đổi quản lý bộ nhớ, giảm lỗi và cải thiện hiệu năng chương trình.

Luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" được bảo vệ tại trường nào?

Luận án này được bảo vệ tại The University of Utah. Năm bảo vệ: 2006.

Luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" thuộc chuyên ngành gì?

Luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" thuộc chuyên ngành Computer Science. Danh mục: Khoa Học Máy Tính.

Luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" có bao nhiêu trang?

Luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" có 139 trang. Bạn có thể xem trước một phần tài liệu ngay trên trang web trước khi tải về.

Cách tải luận án "Magpie: Garbage Collection chính xác cho ngôn ngữ C" về máy như thế nào?

Để tải luận án về máy, bạn nhấn nút "Tải xuống ngay" trên trang này, sau đó hoàn tất thanh toán phí lưu trữ. File sẽ được tải xuống ngay sau khi thanh toán thành công. Hỗ trợ qua Zalo: 0559 297 239.

Luận án liên quan

Chia sẻ tài liệu: Facebook Twitter