Location>code7788 >text

02. Principles of calculator memory

Popularity:959 ℃/2024-08-01 09:49:19

02. Principles of calculator memory

Catalog Introduction

  • 01.What is memory
    • 1.1 Understanding what memory is
    • 1.2 Memory types
  • 02. Memory System Design
    • 2.1 Memory hierarchical design
    • 2.2 Memory Hierarchy
    • 2.3 Cache Design Ideas
    • 2.4 Virtual Memory Access Memory
  • 03. Memory type
    • 3.1 Classification according to material
    • 3.2 By Chip Type
    • 3.3 Memory vs CPU
    • 3.4 Memory Access Rights
    • 3.5 User State vs Kernel State
    • 3.6 Case mmap memory mapping
  • 04. Understanding the principle of localization
    • 4.1 Temporal localization
    • 4.2 Spatial localization
  • 05. Memory Details
    • 5.1 CPU Registers
    • 5.2 CPU Cache
    • 5.3 Memory
    • 5.4 Hard disk
  • 06. Computer address mapping
    • 6.1 What are the address mappings
    • 6.2 Direct Mapping
    • 6.3 Block Mapping
    • 6.4 Multi-level mapping
    • 6.5 Hash Mapping
    • 6.6 Analysis of reflection questions

01.What is memory

1.1 Understanding what memory is

  • Computer memory is a hardware device used to store data and instructions in a computer system.
    • It is one of the core components of a computer and is used to store and retrieve data for processing and manipulation.

1.2 Memory types

  • It can be categorized into two types: primary memory (main memory) and secondary memory (auxiliary memory).
    • Main memory (main memory): Main memory is the place in a computer used to store programs and data that are currently being executed. It is the fastest memory in a computer system and the one directly accessed by the CPU.
    • Auxiliary Memory (Auxiliary Memory): Auxiliary memory is used for long term storage of data and programs, as well as as expansion memory when main memory is insufficient. Auxiliary memory is slower to access, but is usually much larger in capacity than main memory.

02. Memory System Design

2.1 Memory hierarchical design

  • Why are memory systems layered?
    • Memory and hard disks are storage devices. In fact, the registers and CPU L1/L2/L3 caches inside the CPU are also storage devices, and they can be accessed orders of magnitude faster than memory and hard disks.
    • So why use RAM and hard disks when you can just expand the storage capacity of the CPU? This brings us to the 3 main performance metrics of memory: speed + capacity + price per bit.
  • In general, the larger the memory capacity the lower the speed, and the higher the speed the higher the price.
    • It's hard to get a "magic memory" that has high capacity, high speed and low price at the same time.
    • Therefore, modern computer systems will use a layered architecture to meet the maximum cost-effectiveness of the entire system in terms of capacity, speed and price.

2.2 Memory Hierarchy

  • In modern computer systems, the general use of "CPU registers - CPU cache - memory - hard disk" four-level memory structure. The capacity gradually increases from top to bottom, the speed gradually slows down, and the unit price gradually decreases.
    • CPU registers: Store data or instructions being used by the CPU. The registers are the closest memory to the CPU controller and operator, and they are the fastest;
    • CPU Cache: Stores the data and instructions that the CPU will use in the near future.CPU Cache is a layer of cache located in the middle of the CPU and memory. The data mobilization between the cache and memory is done automatically by the hardware and is completely transparent to the upper layer.
    • 3. Memory: Stores programs and data that are running or will run;
    • 4, hard disk: storage of temporarily not in use or can not directly use the program and data. Hard disk is mainly to solve the problem of insufficient capacity of the memory system, hard disk speed is slower than the memory, but the capacity of the hard disk can be much larger than the memory, and power outages do not lose data.
  • These memory devices work together in a computer system to provide different levels and speeds of storage capacity to meet the data storage and access needs of the computer.
  • Based on this, local optimization on each level results in a complete memory system:
    • Optimization 1 - CPU Level 3 Cache: When the concept of CPU Cache first appeared, there was only one cache between the CPU and the memory. With the increase in chip integration density, modern CPU Cache has generally adopted the structure of L1/L2/L3 multi-level cache to improve performance;
    • Optimization 2 - Virtual Memory: Programs do not have direct access to physical addresses, but rather to virtual addresses, which need to undergo Address Translation in order to be mapped to the physical address where the data is stored;
    • Optimization 3 - Page Cache: In order to improve read and write efficiency and to protect the disk, the operating system uses a page cache mechanism in the file system.

2.3 Cache Design Ideas

  • Why add cache between CPU and memory?
    • Reason 1 - Bridging the Speed Difference Between CPU and Memory (Primary): Because the speed difference between CPU and memory is so large, modern computers insert a cache between the two that is faster than the memory in order to equalize the speed difference between the two. As long as the information that the CPU needs to use in the near future is transferred to the cache, the CPU can get the information directly from the cache, thus increasing the access speed;
    • Reason 2 - Reduce CPU and I/O device contention for memory access: Since the CPU and I/O devices compete for the same memory bus, it is possible for the CPU to wait for the I/O device to access memory. If the CPU can get the data directly from the cache, it can reduce the competition and improve the efficiency of the CPU.
  • CPU L3 cache
    • image

2.4 Virtual Memory Access Memory

  • In order to meet the multi-process requirements and large memory needs of the system, the operating system uses virtual memory management at this level of memory.
    • When physical memory resources are insufficient, the operating system will follow a certain algorithm to swap out (Swap Out) the recently unused memory to the hard disk, and then swap in (Swap In) the data to be accessed from the hard disk to the physical memory.
    • As for how the operating system manages the relationship between virtual addresses and memory addresses (segmented, page, segment-page), it is completely transparent to the upper level application.

03. Memory type

3.1 Classification according to material

  • 1, magnetic surface memory: in the metal or plastic surface coated with a layer of magnetic material as a recording medium, with a magnetic head in the magnetic layer to read and write operations. Examples include magnetic disks, magnetic tapes, floppy disks, etc., which have been gradually phased out.
  • 2, optical disk memory: in the metal or plastic surface coated with a layer of magneto-optical material as a recording medium, read and write operations with a laser on the magnetic layer. For example, VCD, DVD, etc., has been gradually eliminated.
  • 3, semiconductor memory: memory composed of semiconductor devices, modern semiconductor memory will use the ultra-large-scale integrated circuit technology will be made of memory chips, with a small size, low power consumption, the advantages of fast access speeds, is currently the mainstream memory technology.

3.2 By Chip Type

  • Semiconductor memories can be categorized into two types according to the access method:
  • 1. RAM (Random-Access Memory):
    • It refers to memory that can be accessed by reading or writing to any memory cell through commands, and all information is lost after a power failure.RAM is not as large as ROM, but is much faster than ROM, and is usually used as the main memory of a computer.
  • 2. ROM (Read-Only Memory):
    • It refers to a memory that can only be read, and information is not lost after power failure. With the development of semiconductor technology, on the basis of ROM and the development of EEPROM (electrically erasable read-only memory) and other technologies, they do not meet the ROM read-only naming, but because it is derived from the ROM technology, followed the original name.
    • The HDDs (mechanical hard disk drives) and SSDs (solid state drives) that we are more familiar with today are derivatives of ROMs.

3.3 Memory vs CPU

3.3.1 Access speed comparison

  • Why is memory access speed so much worse than CPU? There are several main reasons why memory access speeds are slower than CPUs:
  • Technology differences: Memory and CPU are implemented using different technologies.
    • Memory typically uses DRAM (Dynamic Random Access Memory) technology, while CPUs use the faster SRAM (Static Random Access Memory) technology.
    • DRAM is relatively slow to access because it requires a complex series of circuitry and access steps to read and write data.
  • Physical Distance: Memory is usually located on the motherboard of a computer, while the CPU is located in a chipset or processor slot on the motherboard.
    • This means that there is a physical distance between the memory and the CPU, and the data needs to be transferred to the CPU over the bus.This physical distance results in latency and transfer time, which reduces the memory access speed.
  • Capacity Difference: Memory usually has a large capacity and can store large amounts of data.
    • In contrast, CPUs have smaller but faster register and cache memory capacities. This is because larger memory capacities require more circuitry and components to implement, and electrical signaling between these components takes more time.
  • Hierarchy: Memory in a computer system is usually organized according to a hierarchical structure, including registers, caches, main memory, and auxiliary memory.
    • Each level of memory has a different speed and capacity, with higher level memories being faster but smaller in capacity, and lower level memories being slower but larger in capacity. This hierarchy is designed to balance the speed and capacity requirements of the memories.
3.3.2 Relevance statement
  • The relationship between memory and CPU is closely linked.
    • The CPU reads instructions and data from memory and processes them. The speed at which memory is accessed is critical to the performance of the CPU, and faster memory provides faster data transfer, which speeds up the operation of the computer.
    • At the same time, the larger memory capacity can hold more programs and data, allowing the CPU to handle more tasks at the same time.
    • Memory is used to store programs and data, providing temporary storage space; while the CPU executes instructions and processes data, and is the core computing unit of the computer. The synergy between them determines the performance and efficiency of the computer.

3.4 Memory Access Rights

  • In a computer system, memory (Memory) access rights can be categorized according to the design and protection mechanisms of the operating system:
    • User Mode and Kernel Mode.
  • The operating system protects and manages system resources by switching the processor from the user state to the kernel state.
    • When an application needs to perform privileged operations or access restricted resources, it must transfer control to the kernel state through a System Call. A system call is a special instruction that allows an application to request specific services and functions from the operating system.
    • By separating the user state from the kernel state, the operating system ensures that applications cannot directly access and destroy critical system resources. This separation provides security and stability while allowing the operating system to effectively manage and protect resources.

3.5 User State vs Kernel State

  • User Mode:
    • In the user state, applications and user processes run. In the user state, programs can only access restricted areas of memory, usually the user space allocated to the application.
    • User-state programs do not have direct access to core operating system functions and sensitive resources, such as hardware devices and operating system kernel data structures.
  • Kernel Mode:
    • In the kernel state, the operating system kernel runs. The kernel state has higher privileges and wider access rights.
    • In the kernel state, the operating system can access and control all hardware resources, memory, and other core functions of the system. Programs in the kernel state can execute privileged instructions and access protected kernel data structures.
  • Background of the birth of MMKV
    • For the business of weibo chat, high frequency, synchronization, large amount of data written to disk needs. Whether using sp, store, or database, as long as the main thread synchronous write to disk, will be very stuck.
    • The solution is: use the underlying method of memory-mapped mmap, which is equivalent to the system opening up a dedicated memory space for a specified file, and changes to memory data are automatically synchronized to the file.
    • To put it plainly, MMKV achieves the goal of "writing to disk" by "writing to memory". The speed of memory is so fast that the time consumption is almost negligible, thus solving the problem of lag caused by writing to disk.

3.6 Case mmap memory mapping

  • Use this case study to understand the speed of MMKV in the user and kernel states, and then understand the memory mapping
    • User state: read a section of data on the hard disk usually need to go through the hard disk - & gt; kernel - & gt; user, that is, the data needs to go through two copies, very inefficient.
    • Kernel state: mmap maps the physical address of the device to the virtual address of the process, so that when the user operates the virtual memory, it is equivalent to operating the physical device, which reduces the number of data copies from the kernel to the user at one time, and thus improves the throughput rate of data.
  • Memory Mapping is a technique for mapping the contents of a file or device into the virtual memory space of a process.
    • Memory mapping mechanisms are provided in many operating systems, including Unix, Linux, and Windows. Among them, mmap (Memory Map) is a system call used to implement memory mapping in Unix and Linux systems.
    • By using mmap, the contents of a file or device can be mapped directly into the virtual memory space of a process, allowing the process to access the contents of the file or device as if it were memory, without the need for explicit read and write operations. This mapping can be read-write or read-only.
  • Advantages of memory mapping include:
    • Simplify IO operations: With memory mapping, the contents of a file or device can be mapped directly into memory, thus simplifying read and write operations on the file or device.
    • Improved performance: The performance of IO operations can be improved because memory mapping allows direct access to the contents of a file or device, avoiding the copying of data and the use of buffers.
    • Shared Memory: Multiple processes can map the same file or device to their respective virtual memory spaces, thus enabling a shared memory communication method.

04. Understanding the principle of localization

  • The Locality Principle is an important concept in computer science
    • It refers to the tendency of data and instructions accessing memory or storage during program execution to be somewhat localized.
  • In the principles of computer composition, the principle of localization is reflected in many strategies that can be consciously related in learning.
    • For example, when managing the CPU cache, in addition to adding the memory data that is currently being accessed to the cache, it is also possible to cache the data from neighboring memories together (i.e., CPU cache lines), which can also improve the cache hit rate.
  • Where is the principle of localization used
    • Optimization techniques such as caching, prefetching and instruction rearrangement are based on the principle of locality.

4.1 Temporal localization

  • Temporal Locality: Temporal locality means that after an instruction or data has been accessed, there is a high probability that it will be accessed again within a short period of time.
    • For example, some functions, looping statements, or variables in a program tend to be called multiple times in a short period of time;
    • Structures such as loops, iterations, and repetitive operations in programs result in repeated accesses to data and instructions. By exploiting temporal locality, computer systems can cache frequently accessed data and instructions in a cache (Cache) to improve access speed.

4.2 Spatial localization

  • Spatial Locality: Spatial locality means that after an instruction or data has been accessed, there is a high probability that data at an address adjacent to it will also be accessed.
    • For example, after the first element of data is accessed in a program, it is often the case that continuing subsequent elements are also accessed.
    • Data structures, arrays, etc. in programs are usually stored contiguously in memory. By exploiting spatial locality, a computer system can prefetch adjacent data or instructions into the cache to improve access efficiency.

05. Memory Details

5.1 CPU Registers

  • CPU Registers (Central Processing Unit Registers)
    • It is a group of high-speed memory units located inside the CPU for storing and processing instructions, data, and intermediate results. Registers are the fastest memory in the CPU and are accessed faster than main memory (RAM).
  • Case Notes
    • A common example is a calculator program. Calculator programs need to perform a variety of arithmetic and logical operations, which usually involve the use of CPU registers.
    • CPU registers are used to store operands and calculation results. By loading data into the registers, the CPU can access and process this data faster, thus improving the performance of the calculator program.
  • Suppose we have a simple calculator program that performs addition and subtraction operations. The following is an example of a calculator program that uses CPU registers:
    • Initialize registers: At the beginning of the program, the registers to be used are initialized to appropriate values. For example, we can use two general-purpose registers (e.g., R1 and R2) to store operands and results.
    • Input Operands: The user enters two operands to be calculated. These operands can be stored in specific locations in memory and then accessed by loading them into registers.
    • Addition operation: load the first operand into one register (e.g. R1) and the second operand into another register (e.g. R2). The values in these two registers are then added together using the CPU's add instruction and the result is stored in another register (e.g. R3).
    • Subtraction operation: Similarly, the first operand is loaded into one register (e.g. R1) and the second operand is loaded into another register (e.g. R2). The values in these two registers are then subtracted using the CPU's subtraction instruction and the result is stored in another register (e.g. R3).
    • Output result: The result obtained from the calculation is taken out of the register (e.g. R3) and displayed to the user.

5.2 CPU Cache

  • CPU Cache is a type of high-speed memory in a computer system, located inside the CPU or tightly integrated with the CPU.
    • It is used to increase the speed of CPU access to memory data and reduce the number of accesses to main memory.
  • The design of the cache is based on the principle of locality, which means that programs tend to access nearby memory locations.
    • The cache stores recently or frequently accessed data and instructions by creating a smaller but faster storage hierarchy between the CPU and main memory.
    • When the CPU needs to access data, it first checks the cache and if the data is in the cache, it can be fetched quickly. If the data is not in the cache, it needs to be fetched from main memory and loaded into the cache for subsequent access.
  • Caches are usually categorized into multiple levels, such as L1, L2, and L3 caches.
    • The L1 cache is the cache closest to the CPU core and is the fastest but smallest. the L2 cache is located after the L1 cache and is larger but slower. the L3 cache is a larger but slower cache that is usually located outside the CPU core and shared by multiple cores.
  • Case Notes
    • A common case to illustrate the role of CPU cache is matrix multiplication operations. Suppose we have two matrices A and B and need to compute their product C. Matrix multiplication involves a lot of data reading and computation operations, so it can be used to illustrate the importance of the cache.
  • Here is a simplified example (to help understand the CPU cache):
    • Initialize the matrix: load the elements of matrices A and B into main memory.
    • Calculating the product : In calculating the product C, the CPU reads the elements of matrices A and B from main memory and stores them in the cache. Due to the limited capacity of the cache, only part of the data can be stored.
    • Data Access: When computing the product C, the CPU needs to access the elements of matrices A and B repeatedly. If the required elements are already present in the cache, the CPU can access them quickly, thus increasing the speed of computation. This is because the cache can be accessed much faster than main memory.
    • Cache hits and cache misses: When the CPU needs to access an element, it first checks whether the element exists in the cache. If it does, it is called a cache hit and the CPU can read the element directly from the cache. If it does not exist, it is called a cache miss, and the CPU needs to read the element from main memory and store it in the cache for subsequent accesses.

5.3 Memory

  • Memory (RAM) is a hardware device used to store data and instructions in a computer system. It is the primary memory in a computer system and is used for temporary storage of programs and data being executed.
    • Memory is usually based on semiconductor technology, such as dynamic randomized memory (DRAM) or static randomized memory (SRAM). It provides fast read and write access speeds, enabling the CPU to read and write data quickly.
    • It is important to note that memory is a volatile memory, which means that when a computer system loses power, the data in the memory will be lost. Therefore, in order to store data for long periods of time, it is often necessary to use secondary memory (such as a hard disk drive or solid state drive).
    • Memory management is one of the important functions of an operating system, which is responsible for allocating and managing memory resources and ensuring memory isolation and protection between different programs and processes.
  • There are many use cases for computer memory
    • PROGRAM EXECUTION: When programs on a computer are executed, their instructions and data need to be loaded into memory. Memory provides a temporary storage space that allows the CPU to quickly access and process these instructions and data. Memory is constantly read and written to during the execution of a program.
    • Data Storage: Computer memory is also used to store data. For example, when you write an article in a document editor, your input and edits are temporarily stored in memory. The fast read and write speeds of memory allow you to edit and view documents in real time.
    • Multitasking: When a computer is running multiple programs at the same time, memory is used to store the instructions and data of those programs. The operating system loads different programs into different areas of memory and allocates a certain amount of memory space for each program. In this way, each program can run independently without interfering with each other.
    • Graphics and multimedia processing: Computer memory plays an important role in graphics and multimedia processing. For example, when you play a video or run a graphics-intensive game, video and image data needs to be loaded into memory for processing and display.

5.4 Hard disk

  • Hard Disk Drive (HDD) is one of the common storage devices used in computer systems for long-term storage and retrieval of data.
    • Persistent storage: Data on the hard disk can be stored for a long period of time and will not be lost even if the computer is turned off or power is lost.
  • Common cases of computer hard disk usage
    • File Storage and Management: The hard disk is used to store various files created by the user, such as documents, pictures, audio, video, and so on. Users can organize files in different directories and folders on the hard disk for easy management and retrieval.
    • Multimedia storage and playback: Hard disks provide the ability to store and playback a variety of multimedia content. Users can store multimedia files such as music, movies, and television programs on the hard drive and play and watch them using the appropriate applications.

06. Computer address mapping

6.1 What are the address mappings

  • In a computer system, address mapping is the process of mapping logical (or virtual) addresses to physical addresses. The following are a few common types of address mapping:
    • 1. Direct mapping;
    • 2. Block Mapping
    • 3. Multi-level mapping
    • 4. Hash mapping

6.2 Direct Mapping

  • Direct Mapping: Direct Mapping is the simplest way to map addresses.
    • It maps a range of logical addresses directly to the corresponding location of a physical address.
    • For example, logical address 0 maps to physical address 0, logical address 1 maps to physical address 1, and so on. This type of mapping is suitable for address spaces with a fixed size.
    • In direct mapping, each main memory block can only be mapped to a specific location in the cache, which is calculated by taking the address of the main memory block and the size of the cache.
  • What are some examples of direct computer mapping
    • CPU Cache: A computer's CPU cache typically uses direct mapping to speed up data access. Blocks of data in the main memory are mapped to specific locations in the cache for fast access. Each block of main memory can be mapped to only one location in the cache.
    • Database Cache: The cache in a database system can use direct mapping to improve query performance. Chunks of data in the database are mapped to specific locations in the cache for quick access to frequently used data. Each data block can be mapped to only one location in the cache.
  • Direct Mapping Advantages and Disadvantages Analysis
    • The advantage of direct mapping is that it is simple and easy to implement, but the disadvantage is that mapping conflicts may occur, resulting in a lower cache hit rate.

6.3 Block Mapping

  • Block Mapping: Block mapping divides a logical address into fixed-size blocks and maps each block to the corresponding block of a physical address.
    • This type of mapping improves address space utilization and reduces the size of the mapping table.
    • Common block mapping methods include Page Table Mapping and Segment Table Mapping.
    • It is used to map blocks in logical address space to blocks in physical address space. In block mapping, logical blocks are mapped to physical blocks for data storage and access.
  • What are some examples of computer block mapping
    • Filesystem: In a filesystem, files are divided into logical blocks, each of which is mapped to a physical block on a storage device such as a hard disk. A file system uses block mapping to manage file storage and access in order to quickly locate and read file data.
    • Disk Image: In a virtualized environment, a disk image file is typically divided into multiple logical blocks, each of which is mapped to a physical block on the host. This block mapping allows virtual machines to access and manage the data in the disk image file.
  • Pros and cons analysis of block mapping
    • The advantage of block mapping is that it enables efficient data storage and access, but the disadvantage is that mapping conflicts and fragmentation may occur.

6.4 Multi-level mapping

  • Multilevel Mapping: Multilevel mapping is a hierarchical mapping method that maps logical addresses to physical addresses.
    • It divides logical addresses into multiple levels and uses multiple mapping tables for address mapping. This type of mapping reduces the size of the mapping tables and increases the efficiency of address translation.
  • What are some examples of computer multilevel mappings
    • Virtual Memory Management: In an operating system, virtual memory management uses multiple levels of page tables to implement the mapping of logical addresses to physical addresses. The logical address space is divided into multiple levels of page tables, and each level of page table is responsible for mapping a portion of the address space. This multi-level mapping allows the operating system to manage a large range of virtual memory, and to dynamically allocate and reclaim it as needed.

6.5 Hash Mapping

  • Hash Mapping: Hash mapping uses a hash function to map logical addresses to physical addresses.
    • It does this by calculating the hash value of a logical address and mapping it to the corresponding location of a physical address. This mapping provides better address distribution and lookup efficiency.
  • What are some examples of computer hash mapping
    • Hash mapping in computers is commonly used to implement Hash Table data structures for efficient storage and retrieval of data.
    • Suppose we have a hash mapping that stores student information, where the student's name is used as the Key and the student's score is used as the Value. We can use the student's name to compute the hash value and map it to the corresponding physical address.
    import ;
    
    public class StudentHashMapExample {
        public static void main(String[] args) {
            // Create a hash map
            HashMap<String, Integer> studentMap = new HashMap<>();
    
            // Adding student information to a hash map
            ("Alice", 85);
            ("Bob", 92);
            ("Charlie", 78);
            ("David", 90);
    
            // Getting Student Information
            int aliceScore = ("Alice");
            ("Alice's score: " + aliceScore);
    
            // Updating Student Information
            ("Alice", 88);
            aliceScore = ("Alice");
            ("Updated Alice's score: " + aliceScore);
    
            // Deleting Student Information
            ("Bob");
            ("Bob's information removed.");
    
            // Iterating over student information
            for (String name : ()) {
                int score = (name);
                (name + "'s score: " + score);
            }
        }
    }
    

6.6 Analysis of reflection questions

  • Does Java create object references to find objects by direct mapping?
    • In Java, the references that create an object are not found by direct mapping of the object.Object references in Java are actually values that point to the address of the object in heap memory.
    • When we create an object using the new keyword, Java allocates memory space for the object in heap memory and returns the address of the object in heap memory. We can assign this address to an object reference variable to create a reference to the object.
    • Object reference variables store the address of an object in heap memory, rather than mapping directly to the object. When we access an object's member variables or methods through an object reference variable, Java finds the object's location in heap memory based on the address stored in the object reference variable and performs the corresponding operation.
    • In short, object references in Java are implemented by storing the address of the object in heap memory, rather than finding the object by direct mapping.

More content recommendations

  • GitHub:/yangchong211
  • Blog:/user/1978776659695784
  • Blog Roundup:/yangchong211/YCBlogs
  • Design Patterns Column:/yangchong211/YCDesignBlog
  • Java Advanced Advance column:/yangchong211/YCJavaBlog
  • Network Protocols column:/yangchong211/YCJavaBlog
  • A column on basic computer principles:/yangchong211/YCComputerBlog